Listbox.SelectedRowIndex can fail to change

I am seeing the highlighted row number in a listbox being different from the Listbox.SelectedRowIndex.
This is a listbox with less than 10 rows.
The numbers usually match, but clicking one row quickly after clicking another row can leave the SelectedRowIndex unchanged from the previous row’s index.

I included the CellPressed event to the listbox to force the SelectedRowIndex to be correct. CellPressed has always returned the correct row so far.

Function CellPressed(row As Integer, column As Integer, x As Integer, y As Integer) Handles CellPressed as Boolean
  If row<>VisitBox.SelectedRowIndex Then
    VisitBox.SelectedRowIndex=row
  end if
End Function

Is there some caveat to using SelectedRowIndex to test for the index of the highlighted row?

Thanks,

If you have multiple rows selected, SelectedRowIndex returns the number of the lowest row selected, not the last row selected.

Sometimes it’s better to use a loop and check if me.rowselected(row) = true

Windows? Mac?
Do you have a sample project? I can’t reproduce on Mac, maybe I’m missing something.

I should have said this is on a Mac and the listbox is in single row selection mode.
I am trying to reproduce this problem in a sample project to post. No success so far.

I’m able to reproduce it.

In this case, CellPressed fires before the SelectionChanged event, so the row and SelectedRowIndex can be different in the CellPressed event.

Order of events:
CellPressed fires : passes row, column, x, y
SelectionChanged fires: changes the SelectedRowIndex.

Using a Mac, 15.6, Xojo 2025.2.1

1 Like

That makes sense. You can return True in that event to prevent further processing of the click, which includes changing the selection.

The root of the problem is described here: SelectionChanged unreliable with long double click intervals

The macOS mouse double-click speed affects how clicking from row to row is read by Xojo.

Adding this to the listbox CellPressed event forces the SelectedRowIndex to update correctly.

Function CellPressed(row As Integer, column As Integer, x As Integer, y As Integer) Handles CellPressed as Boolean 
    MyListBox.SelectedRowIndex=row 
End Function

You say that quickly clicking to change the selection sometimes leaves SelectedRowIndex lagging behind. Where in your code are you accessing this property?

The property MyListBox.SelectedRowIndex was examined in the MyListBox.SelectionChanged event. I could press between the up and down arrows as fast as I could and the MyListBox.SelectedRowIndex would always be correct. Relying on the mouse click was iffy. Even though the MyListBox.SelectionChanged event fired, the SelectedRowIndex did not necessarily keep up.

When I set the macOS double-click speed to fast, the problem almost went away, but since user settings can’t be anticipated, it looks like forcing the MyListBox.SelectedRowIndex is a safeguard.

This doesn’t surprise me at all. The problem is in the OS logic that decides whether the click is a double click or not. Typically you don’t want to wait the entire double click period to say that a click has happened, especially for extra long double click periods, so it’s kinda catch 22.