ListBox.RowFromXY

From Xojo Documentation

Method

ListBox.RowFromXY(X as Integer,Y as Integer) As Integer

Supported for all project types and targets.

Returns the row index from the passed mouse x, y pixel coordinates.

Notes

The parameters X and Y are relative to the top, left corner of the ListBox. If you use System.MouseX or System.MouseY to get the mouse coordinates to use with this method, you'll need to take into account that those System values are relative to the top-left corner of the entire screen.

Examples

This code in the DoubleClick event of a ListBox added to a Window obtains the indexes of the cell that was double-clicked:

Var xValue As Integer
xValue = System.MouseX - Me.Left - Self.Left // Calculate current mouse position relative to top left of ListBox

Var yValue As Integer
yValue = System.MouseY - Me.Top - Self.Top // Calculate current mouse position relative to top of ListBox.

Var row, column As Integer
row = Me.RowFromXY(xValue, yValue)
column=Me.ColumnFromXY(xValue, yValue)

MessageBox("You double-clicked in cell " + row.ToString + ", " + column.ToString)

If the ListBox code is not on the window (perhaps it is in a subclass or ContainerControl), then you need to be sure to calculate the correct offsets. This code would be used for a ListBox on a ContainerControl:

Var xValue As Integer
xValue = System.MouseX - Me.Left - Self.Left - Me.TrueWindow.Left // Calculate current mouse position relative to top left of ListBox

Var yValue As Integer
yValue = System.MouseY - Me.Top - Self.Top - Me.TrueWindow.Top // Calculate current mouse position relative to top of ListBox.

Var row, column As Integer
row = Me.RowFromXY(xValue, yValue)
column=Me.ColumnFromXY(xValue, yValue)

MessageBox("You double-clicked in cell " + row.ToString + ", " + column.ToString)

See Also

ListBox.ColumnFromXY, System.MouseX, System.MouseY