MouseMove and Selecting ListBox Rows

I use the code shared below in

ListBox1.MouseMove

But, when I move the MouseCursor vertically, some Rows are not selected.

I got the same misbehavior using a different technic (and I forgot which one / I never reported it). I even tried to select * individual Cells à la “Meeting Maker“ and I get the same misbehavior: some Cells were not selected.

  • Using a background color that change at select time followng the pressed keyboard key (Cmd/Windows, Alt, ctrl, Shift gives different colors). This was a test project buried on a hard disk, somewhere. I even lost the screen shot I’ve done that displayed the concept.

Used code:
Sub MouseMove(X As Integer, Y As Integer)
Dim Loc_Row As Integer

  // Get the Row currently below the MouseCursor
  Loc_Row = Me.RowFromXY(X, Y)
  
  // Select the Row
  If x > Me.Left And x < (Me.Left + Me.Width) And y > Me.Top And y < (Me.Top + Me.Height) Then
    // ---------- ---------- ---------- ----------
    // Clear the previously selected Rows
    // Removed
    
    // ---------- ---------- ---------- ----------
    // Select the Row below the MouseCursor
    Me.Selected(Loc_Row) = True
  End If
End Sub

There is no way this will work the way you think. You’re comparing coordinates of the control, which are local to the window, to coordinates of the mouse, which are local to the control.

RowFromXY will always give you the current, not necessarily the selected, row. Just be sure to check for -1, which means the mouse is not over an actual row.

1 Like

Hi Dale,

the line below is not needed to display the trouble.

A. I just made the test. It is here only to avoid Row selection when the MouseCursor is out of the ListBox boundaries. (This is why I add it, but I checked right now to be sure).

B. I rechecked (in case the first was incomplete), and with the if line, I am able to get the first Row selected until the last Row wherever I place the MouseCursor nside the ListBox.
This may be an error I could have done, but, fotunately it is not the case.

At last, Me in the above code refers to the ListBox as well as X,Y: the code is in the ListBox’s MouseMove Event.

Unles I am totally wrong and this can be at 21H… :wink: .

There used to be an issue where RowFromXY would return -1 if over the header and that could cause an OutOfBoundsException in ListBox.Selected, but that appears to be fixed now. I tested with a header row and multiple columns just to be sure I was covering my bases, and added the following code to a ListBox and it selects all rows as I move the mouse over them:

Sub Open() Handles Open
  for i as Integer = 0 to 100
    me.AddRow( Str( i ) )
    var li as Integer = me.LastAddedRowIndex
    me.Cell(li, 2) = "Col2"
    me.Cell(li, 3) = "Col3"
  next
End Sub
Sub MouseMove(X As Integer, Y As Integer) Handles MouseMove
  me.Selected(me.RowFromXY(X, Y)) = True
End Sub

I’m not sure I understand the issue, but the long conditional statement really shouldn’t be needed, as @Dale_Arends said, and I can’t see a problem with some rows being unselectable via this method.

True but they don’t have the same reference. In a ListBox, for example, Me.Top is the location of the top of the ListBox in its parent window while X (and Y) in the MouseMove event is the location of the mouse within the ListBox and has nothing to do with the location of the Listbox in the window. Conversely, when the MouseMove event has a value for X and Y, it has no bearing on where in the window the ListBox is.

Perhaps I’m just not understanding what you’re trying to do.

2 Likes

Thank you Anthony for your kind answer.

I checked: no OutOfBounds (fortunately). My ListBox have 11 Columns.

Yesterday’s problem was:
Move the Mouse from the Window’s title until its bottom.
When the MouseCursor is above the ListBox, even when I do not move slowly (not fast, just above slow: normally I would say), not all Rows are selected: some are not.

I’m not sure I understand the issue, but the long conditional statement really shouldn’t be needed, as @Dale_Arends said
YOU ARE RIGHT !

And I tested, when I am in Firefox (for example), moving the MouseCursor above the running project (in the IDE), does nothing to it !
Scrolling using the MousePad (is it its name ?) and two Fingers… moves as usual the ListBox contents. My MS Mouse does not scroll since the down of its life… so no test for that, but I guess…

The application runs in a Windows XP who do not have Internet access and so I compile it using Xojo 2015r1.
Here is a MacBook Pro with El Capitan :wink: