Colour a Cell Background in a Listbox?

Hey guys,

So I want to apply a colour to a cell in a listbox row. (not the entire row)

 g.ForeColor = &cf3f6fA
 g.FillRect(0, 0, g.Width, g.Height)

This is code I ‘borrowed’ from a CellBackgroundPaint event when I create the listbox, but once that’s done, how do I selectively change the background colour of a specific cell?

Thanks in Advance…

if row=the_row_i_want_to_color and column=the_column_i_want_to color then 
g.ForeColor = &cf3f6fA
g.FillRect(0, 0, g.Width, g.Height)
end if

Where does ‘g’ come from?

1 Like

Have you read the LangRef concerning Listbox events?
If not, please do so.

Thanks Dave, yes I have and maybe I’m just not getting it? :frowning:

I understand that while inside the CellBackgroundPaint event, I have access to ‘g’ as a parameter and I can set/get it, etc. What I want to do is outside of that event and what I’m struggling with is: How do I get the graphics object (‘g’) if I’m not in an event that provides it for me, i.e.,

Dim g as graphic = listbox.cell(1,1).???

Sorry to be annoying, but I have read a bit about both the listbox and graphic objects and I’m still unclear?


Short answer: you don’t. Instead you cause the event to happen by using the second part of Dave’s first reply. When you call .InvalidateCell() on a specific cell (from outside that event) then Xojo will call the CellBackgroundPaint event for you.

So your CellBackgroundPaint() event needs enough logic to know what colors and drawing you want to do, but you can modify a CellTag property or anything else needed to the event can decide what it should do when the cell must be (re)drawn for whatever reason. One of those reasons can be using .InvalidateCell() as in Dave’s first reply .

The CellBackgroundPaint and CellTexdPaint events can pass their “G” graphics objects to an outside method, since it is simply a “pointer”… but you cannot access it from outside. By that I mean, the Listbox events can give “G” to another method to use, but another method cannot query the Listbox to retrieve the graphics object.

This same logic/restriction applies to the CANVAS object as well (and technically a listbox cell IS a canvas)

Thanks guys. :wink: