How to color text in a listbox cell

I’m finding it hard to understand how to set the color of text in a particular listbox cell as I populate the Listbox. For example I might read something like

This is a new section”

as a string from a file, then remove the tags at the beginning and make the cell text bold and red. Can someone explain how I do that? Many thanks

Yes, of course I’ve seen the manual but as I said I am finding it difficult to understand that particular event, which is why I am asking the question in the ‘Getting Started’ section.

The event opens the graphics object of the listbox cell to you.

Using the parameters provided (Row and Column) you can get the cell content to determine how you would format it. So if your cell content was to be bold and red in the event you would change these properties on the graphics object during the CellTextPaint event.

g.Bold = true g.ForeColore = &cff0000

Hi Tim, thanks for responding and I apologise for saying I posted in the Getting Started section - I see I posted in the General section by mistake.

Here is the code I have written to put all my tab-separated text in a listbox

dim s1, s2 as string dim n, u as integer u=UBound(sA)//sA declared earlier, sA array contains all the strings to populate the listbox for n = 0 to u s= sA(n)//s was declared earlier s1=NthField(s,tab,1)//tab has been defined as chr(09) s2=NthField(s,tab,2) cc1LB.AddRow s1//cc1LB is the listbox if s2="" then//if s2 is blank then s1 contains a heading I would like to be bold and red cc1LB.celltag(n,0)="boldred"//As I understand it, this gives the CellTextPaint event something to work with end cc1LB.Cell(cc1LB.LastIndex,1)=s2//This adds the text to the second cell if there is any text in s2 next//Continues to populate the listbox

This works. My understanding breaks down with the CellTextPaint event of the listbox. The best I have managed so far is this - and it is obviously full of errors

dim g as graphics//error dim row, column as integer//error dim x, y as integer//error if me.CellTag="boldred" then//error g.ForeColor=&cff0000 g.bold=true end

//I needen’t go on, this isn’t working at all

I obviously have some fundamental misunderstanding about how to make the event work for me.

if me.celltag (row, column) = “boldred” then

And do not dim anything. Those variables are supplied as parameters to the event.

Thank you very much Tim Parnell and Tim Hare. After I last posted, it gradually dawned on me that I didn’t need to declare the variables, I just had to use the variable names in the event. With the declarations deleted and me.celltag(row.column) everything is now working fine. Thanks and sorry for the confusion

Steve… the point here is, you learned something, and you realized why what you had attempted before wasn’t working, and based on the info from Tim*2 you ended up in the right place. That is what this forum is all about.


Is Tim*2 greater than the sum of the individuals? :slight_smile: