Part of a Listbox Row in a different color

I want to show search results in a listbox.
The part of the line that matches the search term should be drawn in different color.

For example:
searchterm “hugo”

Listbox rows:
My friend
My friend hugo
My friend hugo goes
nothing red
My friend hugo goes to hugos house
again nothing red.

if it´s not possible to draw in a different colur, maybe it´s possible to draw the "hugo"s in bold

You can use the ‘CellBackgroundPaint’-Event to change the color of an individual cell. For Example:

if row = 1 and column = 3 then g.foreColor=rgb(255,0,0) g.FillRect(0,0,g.width,g.height) end if

thanks
If I understand right, that draws the background of a whole cell.
I just want to have single words in acell coloured.
In my example the listbox has just 1 column

[quote=88703:@michael eder]thanks
If I understand right, that draws the background of a whole cell.
I just want to have single words in acell coloured.
In my example the listbox has just 1 column[/quote]

CellBackgroundPaint lets you draw whatever (and wherever) you want in the cell. That said since you are doing text I would suggest you draw the the text with word highlight in CellTextPaint.

You will just need to break the string into pieces and draw each piece any way you want. Use g.StringWidth to measure the pieces

This code inside the CellTextPaint Event should write the strings “blue” and “red” in the appropriate colors in the first row:

If row = 1 then g.ForeColor=rgb(255,0,0) g.DrawString("red",0,10) g.ForeColor=rgb(0,0,255) g.DrawString("blue",20,10) end if

second row of course

Thanks!
Works Perfect

how can I mark this Question answered?

When you point at the post that answered it, symbols appear. One of them marks it as the answer.

Make sure you return true after drawing the text yourself so it won’t be overwritten by the contents of the cell.

Tnx Tim,

how can I avoid to call n times AddRow for the numbers of rows I need, because I don’t want to show the cells context.
If I don’t call AddRows the CellTextPaint Event isn’t called.

[quote=88890:@michael eder]how can I avoid to call n times AddRow for the numbers of rows I need, because I don’t want to show the cells context.
If I don’t call AddRows the CellTextPaint Event isn’t called.[/quote]
I don’t think you can. I think the only way to set the number of rows in a Listbox is to use the AddRows method the number of times you need.

Why do you need a fixed number of rows? What do your users do with the blank rows?

I want to render the rows only with the CellTextPaint Event, so the AddRow is useless, exept to tell the Listbox how many rows it has.

You can use CellBackgroundPaint instead. It gets called for all visible cells, whether there is anything in them or not.

You can’t have rows if you don’t add them. AddRow adds the row, without it there’s no row to have and render.

In the paint events for background and text you can control exactly what gets painted (which can be completely unrelated to the content) or that nothing is painted (return true without drawing anything).

If you want full control return true in CellTextPaint and do all the drawing (even text, if you wish) from within CellBackgroundPaint.