Manually Drawing Listbox Borders

I am trying to create a custom listbox class (subclass of listbox) and have figured out how to draw left and right borders in CellBackgroundPaint event but can’t work out how to draw the top and bottom borders whether the listbox has contents or is empty.

The complex issue is trying to determine the first visible row and then drawing the line at the top of this graphics object for the row and determining the last visible or partially visible row and drawing the bottom line.

And if there are no rows or some rows but not all the way to the bottom of the listbox it becomes even harder or impossible as it stands for me right now.

I know I could go the container control route and place the listbox on a rectangle object or create separate border canvas or rectangle objects around it but I’d rather not do that if possible.

Any ideas?

just draw a line at 0,g.height -1,g.width, g.height-1

If you draw top AND bottom, you get twice the line thickness.

This won’t work Jeff. the graphics object is per cell not for the entire listbox so this will simply draw a line at the top of a specific cell and so would be at the top of all cells, not just the first visible and the last visible.

Sorry: that wasnt clear from the first post.
Not actually sure what you want, then

On Mac you could lay a canvas on top of the listBox to draw on.

On Windows the canvas consumes events, so it would not work.

Top visible row = ListBox.ScrollPosition
LastRow = ListBox.ListCount -1

The window of your listbox has a paint event.

Is there a reason you cannot use the built-in properties the control the cell borders?

Thanks for all your comments which helped a lot. I’ve worked it out and this works:

[code] // Draw top border
If row = Me.ScrollPosition then
g.DrawLine(0, 0, g.Width, 0)
End If

Dim visibleRows As Integer = Floor(Me.Height/Me.RowHeight)
Dim gapHeight As Integer = Me.Height - (visibleRows * Me.RowHeight)

// Draw bottom border
If gapHeight > 0 and row > (visibleRows-1) + Me.ScrollPosition then
g.DrawLine(0, gapHeight-1, g.Width, gapHeight-1)
ElseIf gapHeight = 0 and row = (visibleRows-1) + Me.ScrollPosition then
g.DrawLine(0, g.Height-1, g.Width, g.Height-1)
End If[/code]