Graphics draw an object in specific listbox cell

I would like to add a drawn object, such as a round rectangle, to a specific column in a listbox. I have a listbox with 4 columns. The first 3 have text, and I would like the 4th column to have an image which would be a drawn button. But I’m not sure how to specify only the 4th column gets the drawing

In CellTextPaint (not sure if this is the right place for it), I have something like this

g.DrawingColor = color.Blue
g.DrawRectangle(0, 0, g.Width / 2, g.Height / 2)

But this is adding a blue rectangle to each of the cells, and only the ones containing text

You need to add a conditional statement using the column parameter:

If column = 3 Then
  g.DrawingColor = color.Blue
  g.DrawRectangle(0, 0, g.Width / 2, g.Height / 2)
End If

Thanks Anthony. That works, but it only adds the drawn image when text is present. Does this need to go elsewhere other than the CellTextPaint?

Please don’t judge the image. Just trying to get things to work before prettying up :slight_smile:

Move the code to the CellBackgroundPaint event.

Function CellBackgroundPaint(g As Graphics, row As Integer, column As Integer) Handles CellBackgroundPaint as Boolean
  If column = 3 and row <= me.LastRowIndex Then
    g.DrawingColor = color.Blue
    g.DrawRectangle(0, 0, g.Width / 2, g.Height / 2)
    
    Return True
  End If
End Function

That does the trick! Thank you for the help!

Happy to help.