Listbox text color

Is there a way to change the text color in a listbox?

Use the CellTextPaint event, for example:

[code]If column = 1 Then
g.ForeColor = &cFF000000
End If

Return False
[/code]

That will make text in column 1 red. You can read up on that event, why you may return True or False in a given situation, etc… in the docs: http://documentation.xojo.com/index.php/ListBox.CellTextPaint … you may also be interested in its companion event, CellBackgroundPaint.

Thank you Jeremy.

Remember you need to handle selected color if the row is selected

Ah, Karen is Right. A lot of times I’ll just do:

If column = 1 And Me.ListIndex <> Row Then // blah blah blah End If

This lets the system paint the color when the row is selected.

When it comes to the listbox i usually am. :wink:

BTW did you get my PM on listbox stuff? The software said it was not sent so I sent it 3 times… When i saw that I deleted 2 of them…

The PM code for this board is a little wonky!

[quote=17530:@Jeremy Cowgar]

If column = 1 And Me.ListIndex <> Row Then // blah blah blah End If

This lets the system paint the color when the row is selected.[/quote]

Here you are assuming multiple selection is not enabled. It would be better to do:

If column = 1 And NOT me.Selected(Row) Then // blah blah blah End If

Nope. I sent you an email, so you can reply to that. Thanks!

I could be wrong, but I have always read in high traffic areas to break up AND statements. Wouldn’t this do less checking?:

If column = 1 Then If NOT me.Selected(Row) Then // blah blah blah End If End If

With the AND aren’t you checking the second half, NOT me.Selected(Row), on every column? In a small ListBox it won’t make much of a difference, but on a list box with 50 columns seems like it would do a lot of unnecessary checking.

No, Xojo does short-circuiting in conditional statements. For example,

[code]Dim a() As String

If False And a(100).Len = 0 Then
MsgBox “Hi”
End If
[/code]

a(100).Len = 0 would produce an out of bounds error, but if you run the above code, all work work fine. The reason is that Xojo tests the first case (False) and sees it evaluates to False and skips checking any more. See: Short-circuit evaluation - Wikipedia for more information on Short Circuiting.

Thanks. Odd, but in one of the old RB Developer articles about speed tricks, it says to not do this:

If MyBoolean = True Then

but rather do this:

If MyBoolean Then

because it doesn’t have to evaluate both sides, the boolean and the True statement. Seems like it would be able to short-circuit that as well. This is why I write database code and not compilers.

[quote=17540:@Melvyn Pate]Thanks. Odd, but in one of the old RB Developer articles about speed tricks, it says to not do this:

[code]If MyBoolean = True Then

// but rather do this:

If MyBoolean Then
[/code]

because it doesn’t have to evaluate both sides, the boolean and the True statement. Seems like it would be able to short-circuit that as well. This is why I write database code and not compilers.[/quote]

That may be the case. I did a bit of profiling and across 1,000,000 iterations, the If Blah Then was about 10ms after on average. I do not know the internals of Xojo’s compiler, but if you provide an equal sign, then I would assume it has to see what is on the other side. For example, is it MyBoolean = True or MyBoolean = False. If, on the other hand, you simply say If MyBoolean Then it could potentially do a compile time optimization there, but I am just guessing.

The above code though is different than using an And or Or statement. When using those, you are executing multiple condition checks. That can easily be short circuited. For example, here is another one:

If purchasePrice < bankAccountBalance Or HaveEnoughCredit(owner, purchasePrice) Then

The method HaveEnoughCredit will never be called if the purchasePrice is less than bankAccountBalance because Xojo already knows that the If statement should be executed upon doing the first compare. Doing any more work is not needed.

OK, thanks. Figure out what works best and run with…

Oh… Just to be clear, that is 10ms faster across 1,000,000 iterations, not per iteration.

Thanks, that makes sense now I couldn’t figure out what you meant by “about 10ms after on average”. It sounded like you saying “after” as in “later” which would mean it was slower.

Another vote for allowing editing of posts. This scenario happens about 1000 times more than the dreaded, “changing of posts for evil purposes”.