« 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
one comment

I Want to display user list with there color code rectangle box, I think this code is equivalent to my requirement but I tried with same code as you given on this site. But unfortunately the cell is not get colored.
Can you please help me to solved this.
Amol Deshpande - 19 01 17 - 10:26


  
Remember personal info?

Emoticons / Textile


Notify:
Hide email:

Small print: All html tags except <b> and <i> will be removed from your comment. You can make links by just typing the url or mail-address.