Problem determining which listbox row was click

[code] dim iRow As Integer
dim sRecID As String

iRow = Me.RowFromXY(System.MouseX - Me.Left - Self.Left, System.MouseY - Me.Top - Self.Top)
sRecID = lbMasons.cell( iRow, 0 )

tpPanel.Value = 0


I’m totally lost. The above code is in the DoubleClick event of a ListBox. The exact same code is in several other windows with similar ListBoxes and they all work perfectly. In this one window, iRow always ends up -1 regardless of which row is clicked.

I have deleted the ListBox and added back a new one. What do I need to look for to figure out what’s going on?

And just as an aside, why can’t Xojo make it more straightforward to determine which row was clicked?


why not get the row during the CLICK event…which happens just prior to the DOUBLE click event

Tried and got the same results…

then somthing is stealing the focus

I’m not sure why focus would make a difference. The logic of that iRow line should work even when the app is in the background.

My suspicion is the listbox is in a ContainerControl which means more Lefts and Tops need to be subtracted. To pin down whats happening in that line explode out all the parameters and values of interest into local variables for viewing in the debugger (or write them to System.Debuglog).

[code] dim x, x1, x2, x3, y, y1, y2, y3, zw, zh As integer
x1 = System.MouseX
x2 = Me.Left
x3 = Self.Left
y1 = System.MouseY
y2 = Me.Top
y3 = Self.Top
x = x1 - x2 - x3
y = y1 - y2 - y3
zw = Me.Width
zh = Me.Height

iRow = Me.RowFromXY(x, y)


If x or y is less than 0 or greater than their respective width/height then somethings missing.

Also, I think the workaround Dave was suggesting is that you store the row parameter you get in CellClick, not use the RowFromXY function in CellClick if that’s what you’r doing. That row parameter should always be correct.

that is correct

Can’t you use ListIndex?

Is your listbox a subclass of listbox? Then you have to adjust the code:
Listbox on a Window:

iRow = Me.RowFromXY(System.MouseX - Self.Left - Me.Left, System.MouseY - Self.Top - Me.Top // where Self is the window and Me the listbox
Listbox subclass on a Window (this code is within the subclass):

iRow = Me.RowFromXY(System.MouseX - Window.Left - Self.Left, System.MouseY - Window.Top - Self.Top // where Self is the listbox

I still don’t understand why everyone is trying to jump thru hoops for something that is so simple.

  • create a global property… call it CLICKEDROW (as Integer)
  • in the CELLCLICK event of the the listbox put : CLICKEDROW=ROW
  • in the DOUBLECLICK event use the value of CLICKEDROW (you have to single click before you can doubleclick)

I do not use any globals in my applications (besides the ones of the Xojo framework of course).

On more serious note:
There are two answers for William’s question: The easiest solution and the solution (possibly) explaining why RowFromXY didn’t return the result William expected.

I use listbox1.listindex all the time in the change event

This thread is about getting the row in the double click event.

Right, but the ListIndex is already set during the first click (i.e. the Change event). I use this all the time to figure out what row was double clicked.

Going through all these gyrations to figure out the row makes no sense when you already have it. If ListIndex is wrong, it means something else is setting the ListIndex - probably in the Change event. Check the Change event and see where it leads. Or better yet, comment it out and see if the ListIndex is correct.

Though very unusual it is possible for ListIndex to be changed before DoubleClick fires. Storing CellClicks row guarantees getting the clicked row. But that doesn’t answer why Williams RowFromXY code doesn’t work in this one case.

If his code is in a listbox subclass he has to adjust his RowFromXY-code as stated above.

Even if the listbox is a subclass then the ListIndex should still be correct.

I create subclasses of Listbox all the time. Normally I override the DoubleClick event in my subclass and create a new Event Definition called Double click that send the row. In my DoubleClick event that I am overriding i just use:

dim row as integer row = ListIndex RaiseEvent DoubleClick(row)
This usually works fine.