Hello,
How do I add an image (png or svg) to the second column of a ListBox? It seems to me that there’s no parameter to indicate the column (e.g. RowImageAt(row_index), doesn’t take a second parameter for the column). Is there a trick to this? (MBS also possible).
Thanks.
Dan
You can paint in any column. Here I paint in column 0:
const offSet as Integer = 4
const IconSize as Integer = 16
const xOffset as Integer = 16
if column = 0 then
dim AccountPic as Picture = 'get some picture
g.DrawPicture(AccountPic, OffSet + xOffset, (g.Height - IconSize)/2, IconSize, IconSize, 0, 0, AccountPic.Width, AccountPic.Height)
end if
This is done in CellTextPaint. Do not forget to set the text to some string with spaces or you will curse.
OP asked to draw in the second Column,
Changing Colun to 1 or 2 does not display anything… (but your code as is draws the icon).
Not if you draw the text there, use x to do that… x = Offset *2 + 8 (for example).
Emile (who was curious)…
To OP:
I would use,an emoji if this is a hierarchical ListBox Row:
Not the other folders emoji…
available: folder Open, Close for macOS AND windows…
Use Show emojis and others… menu (if you have place Elmoji access in the menu bar)
Curious as to why you’d do this in CellTextPaint and not CellBackgroundPaint.
Good question. I remember having problems with the text not showing correctly for the selected row. But this seems to be gone.
However, should both CellTextPaint and CellBackgroundPaint do the paint at the same location? I forgot to remove the code in text paint and this isn’t even a pixel or 2:
The generic icon is background and the specific icons are text paint. Xojo 2024r1 so I need to check in latest Xojo.
Another benefit of using CellTextPaint is when you want to draw the text and the picture together. Only CellTextPaint provides an x parameter for where the text should start being drawn. If you have an icon to show, you’d want the icon to be drawn first, at the x coordinate, and the text is then drawn with an offset.
That is what I said in an earlier post.
Yes, but Julia’s question was after that post.
In the paint event you can use :
New Picture ( Me.Width , Me.Height , 32 )
' cha est une image
pict.Graphics.DrawPicture ( cha , 0, 0 , pict.Width , pict.Height , 0 , 0 , cha.Width , cha.Height )
Var p_L , p_T , p_R , p_B As Integer
p_L = g.Width * column
p_T = ( g.Height * row )
p_R = p_L + g.Width
p_B = p_T + g.Height
If row < Me.RowCount Then
g.DrawPicture ( pict , 0 , 0 , g.Width , g.Height , p_L , p_T , p_R , p_B )
End If
Exception err
Break
//
This method is to be finished to adjust the image portion