Background color and listbox, again

Hello, I’ve search a lot, tried a lot, almost there but:

I have a listbox, 10 columns X 10 rows, all these will be given a random numder (0-9) in its open even, so that I get a random number in each sell.

Now, I have a textfield, I enter a number here, and now in a pushbutton, I start to scan (loop through all the cells to find each cell that has this number), and count them.

Now I’m trying to use cellbackroundpaint for chaninging background color for each cell that matches, but it is only the last found that stays that color, the rest goes back to white…

What is the best way to solve this ? Please help.

For each cell that matches put the colour you want into the cellTag, if it doesn’t match put white in the CellTag

In background paint event use the colour in the celltag to paint the cell

THanks markus but how to I store it in the Celltag, and how do I make the background paint read it ?

Read up on the ListBox and its CellTag - it is written there

http://documentation.xojo.com/index.php/Listbox

From the School of Charles: try, then ask.

After you paint a cell in the cellBackgroundPaint event, make sure you return True to prevent the system from over-drawing your painting.

This example sets a tag for a cell.
LB.CellTag(1, 4) = myColour

In CellBackgroundPaint you need to cast the CellTag (a variant) to a color:

If me.CellTag( row, column ) <> Nil then g.ForeColor = Color( me.CellTag( row, column ) ) g.FillRect( 0, 0, g.width, g.height ) End if

Since when?

I stand corrected :wink:

In my defence I’m not at my computer and thought it to be better safe than sorry as
I wasn’t sure about implicit conversion to colour anymore.

Thanks, Tim.

So the code is

If me.CellTag( row, column ) <> Nil then g.ForeColor = me.CellTag( row, column ) g.FillRect( 0, 0, g.width, g.height ) End if

But doing this I get error:

OutOfBoundsException

@ this line: If me.CellTag( row, column ) <> Nil then

ideas ?

Well, I just had to AddRow one more time at the end, then it worked (addrow in the open event, outside the loop)

[quote=167218:@Helge Tjelta]But doing this I get error:

OutOfBoundsException

@ this line: If me.CellTag( row, column ) <> Nil then

ideas ?

Well, I just had to AddRow one more time at the end, then it worked (addrow in the open event, outside the loop)[/quote]

You have to restrict your drawing code to the actual rows.

That’s because “CellBackgroundPaint fires for every visible row in a ListBox, regardless of whether there is an actual row there or not.” from the documentation (http://documentation.xojo.com/index.php/ListBox.CellBackgroundPaint).

So you are trying to read the tag from a cell that does not exist. You need to first check whether the row exists, then read the tag.

Your solution will only work in case you don’t ever change the size of the listbox.

I see Markus beat me to it… I will still leave my reply.

Julen

[quote=167221:@Julen Ibarretxe Uriguen]
I see Markus beat me to it… I will still leave my reply.
Julen[/quote]
29 sec between us, but you wrote much more, so technically it is a tie at least. :wink:

To restrict your drawing code:

If row < me.ListCount - 1 then

NICE, Thanks!!!

link text

[quote=167223:@Markus Winter]To restrict your drawing code:

If row < me.ListCount - 1 then[/quote]
Still a tie!

[quote=167223:@Markus Winter]To restrict your drawing code:

If row < me.ListCount - 1 then[/quote]

umm should not that be:

If Row < me.ListCount

[quote=167229:@Karen Atkocius]umm should not that be:

If Row < me.ListCount[/quote]

Karen is right

ListCount is 1 based, but you don’t need the - 1 because of the <

Brain is mush, need sleep now