I have a “button” that I draw in a single column listbox row. When I draw it, I compute its local bounds so I can decide if it’s clicked on.
I use the DesktopListbox.MouseMoved() event to capture the absolute position of the mouse within the listbox when it moved. I can compute the row moved over in this event with the RowFromXY() method. How do I determine the Y coordinate for the row being moved over. The Y coordinate provided in the event is relative to the top left corner of the control but what if the listbox has scrolled?
mLastMouseMoveX = x
mLastMouseMoveY = y
mLastMouseOverRow = Me.RowFromXY(x, y)
If mLastMouseOverRow > Me.LastRowIndex Then mLastMouseOverRow = -1
// Calculate the absolute Y coordinate of the top of this row: visible row offset * row height
Var rowTopY As Integer = (row - ScrollPosition) * DefaultRowHeight
mLastMouseMoveLocalY = absoluteY - rowTopY
But I think ScrollPosition doesn’t account for fractional row scrolling (i.e. the Listbox doesn’t scroll one line at a time).
This can happen when the listbox is scrolled to the very bottom and the height of the listbox isn’t a multiple of the row’s height (disregarding the header, of course).
You guys are right. For some reason I thought partial scrolling was a thing but I’m wrong. The DesktopListbox definitely only scrolls one row at a time.
This works for me in MouseMove():
mLastMouseMoveX = x
mLastMouseMoveY = y
mLastMouseOverRow = Me.RowFromXY(x, y)
If mLastMouseOverRow > Me.LastRowIndex Then mLastMouseOverRow = -1
// Calculate the absolute Y coordinate of the top of this row: visible row offset * row height
If mLastMouseOverRow <> -1 Then
Var rowTopY As Integer = (mLastMouseOverRow - ScrollPosition) * DefaultRowHeight
mLastMouseMoveLocalY = Max(-1, y - rowTopY)
Else
mLastMouseMoveLocalY = -1
End If
Me.Refresh