Listbox - fill listbox row with icon and text


I am drawing an icon in a listbox depending on the icon which is assigned to an entry.
This is how i populate my listbox (single col. listbox);

myrow= RS.Field("Name").StringValue 
lb.AddRow myrow    

Dim Iconstring as String
Iconstring = rs.Field("Iconstring").StringValue

dim bin as BinaryStream
dim mb as MemoryBlock
dim p as Picture

mb = DecodeBase64(Iconstring)
p = Picture.FromData(mb )
lb.RowPicture(lb.LastIndex) = p

This works so far. My problem ist that the icon is left aligned and then followed by the text in the row of the listbox. With ColumenAllignmentOffset it is possible to move the text to the right. But the icon is still left aligned. And if there is no icon present, the text in the row has another “left margin” as the entry with icon.

Is there a way to move the icon to the right ? I tried with CellBackgroundPaint but could not get it to work.
Or do I need to take another approach to fill a listbox column with icon and text ?

Thanks in advance !

Take a look at CellTextPaint. You can control the position of your icon AND the text in one place.

Btw. Remember to return True in this and the CellBackgroundPaint events if you really want them to draw.

I was able to control the position of the text with
lb.ColumnAlignmentOffset(0) = 45
in the CellTextPaint. But I am not able to control the position of the icon. The icon is stored in the listbox by using
lb.RowPicture(lb.LastIndex) = p
where lb stands for the listbox.
How can I control or modify the RowPicture ? To be honest I have no idea left.

Thanks in advance !

Tip: Don’t use the RowPicture its not very flexible at all.

I tend to draw the icon in the listbox in the CellBackgroundPaint event, use g.DrawPicture and then in the CellTextPaint event draw your text in using g.DrawString.

You can then have your icon exactly where you want it just offset it from the left using the x parameter in the g.DrawPicture method and the same with your text.

Note: return true in each of the events or I think the selection color will overdraw your icon and the text will overwrite the text you have drawn. You will have to draw your own selection color though in the CellBackGroundPaint event but it does mean you can do fancy things like gradient selection bars instead of the boring OS purple.

I am using Xojo 1.1
RowPicture does not exist. Am I missing something?

Rowpicture does exist, but… as Mike suggested use the Cellbackgroundpaint event

In the CellBackgroundPaint event I am drawing the icon (picture)

g.DrawPicture p, 10, 4
return true

and in the CellTextPaint event I draw the text with g.drawstring.

[quote=104233:@Metodija Angjelkoski]mb = DecodeBase64(Iconstring)
p = Picture.FromData(mb )
lb.RowPicture(lb.LastIndex) = p[/quote]

Yes you seem to be missing something indeed. Here is your own code with RowPicture in it :

mb = DecodeBase64(Iconstring) p = Picture.FromData(mb ) lb.RowPicture(lb.LastIndex) = p

You can move the icon to the right in RowPicture by using a new picture where you draw the icon on the right. Here is an example with a 16x16 icon (I don’t know the size of p) :

dim pic as new picture(32,16),16,0) ListBox1.RowPicture(row) = pic