« Tip of the day: Hide … | Home | REAL Studio Developer… »

Tip of the day: How to color cells in listbox

As yesterday someone asked for how to color listbox background, I want to share the sample with you. You can do it like this. First when you fill your listbox, you store information in celltag, rowtag or columntag. For example here we fill a line in Listbox1.Open event:
    Sub Open() 
me.AddRow "Hello" // Add some text.
me.Cell(me.LastIndex,1)="World" // Add text to second column

me.CellTag(me.LastIndex,0) = &cFFCCCC // 1. column red
me.CellTag(me.LastIndex,1) = &cCCFFCC // 2. column green
me.CellTag(me.LastIndex,2) = &cCCCCFF // 3. column blue

End Sub
Later in CellBackgroundPaint, we check the celltag for the current cell, and fill the graphics rectangle with that color:
    Function CellBackgroundPaint(g As Graphics, row As Integer, column As Integer) As Boolean
if row >= me.ListCount then
// empty area. We simply make all rows gray

g.ForeColor = &cCCCCCC
g.FillRect 0,0,g.Width,g.Height
Return true

elseif row <> me.ListIndex then // if not selected line (selected is blue)

// Get color from celltag and use it
dim c as color = me.CellTag(Row,column)

g.ForeColor = c
g.FillRect 0,0,g.Width,g.Height
Return true
end if
End Function
02 03 11 - 17:45