Listbox MouseDown/CellClick Question

Hi,

I am trying to capture all rows selected in my listbox and I have this code that works well (listbox.cellclick) event HOWEVER it is a click behind. Is there something I can do differently in my code to get the selection only on the first mouse click?

Example:

  1. Click on Two Rows
  2. DEbug Pops up and shows my array at -1
  3. Press Play on IDE
  4. Click the SAME two rows
  5. Debug shows my array now having the correct rows that were selected…But I had to click two separate times to get it.

for i as integer = 0 to Me.ListCount-1
if me.Selected(i) Then
SelectedRows.Append i
End if
Next i

I have tried this code slightly modified in the Window’s Mouse UP event and Mouse Down event with no success.

Thanks in advance!

Depending on how you are going to use (and ultimately clear the selections)
Why not put code in the CLICK event

if me.selected(row) then 
   selectedrows.append row
else
  selectedrows.remove [figure out the index]
end if

if SelectedRows were a Dictionary it would be real easy as the row could be key

CellClick happens BEFORE the selection is changed. Use the Row parameter to update your array. Or you might be able to use the Change event, although it fires more often than you might want.

Ok thanks guys!! I appreciate the help!

Tim / Dave,

Thanks again guys as I added my code to the Change Event and that is working perfectly catching my clicks/multi clicks in real time.

Thank you!

Using the Change Event solved it for me also. Good.

May I ask, why the MouseUp event also seems to be one click behind?
MouseUp happens after MouseDown etc… -At least from the operators’ side of things.

[quote=160403:@Roger Jönsson]Using the Change Event solved it for me also. Good.

May I ask, why the MouseUp event also seems to be one click behind?
MouseUp happens after MouseDown etc… -At least from the operators’ side of things.[/quote]
Not sure I understand you concern? Of course MouseUp happens after MouseDown… how could it not?
But ONLY if you return TRUE from the MouseDown (otherwise UP never fires at all)

I tried to read a Listboxs’ Listindex, by clicking on a row in it (and then use the listindex to get the data from the row I clicked on).
I couldn’t do this with mousedown and I couldn’t do this with mouse up. I figured that the mouse up would happen so late, that it should not be a problem getting the current listindex (if mouse down was too early to get it). Whatever I did I got the previous listindex. I guessed that it might have to do with timing, but that was obviously not it.

So the question is: Why do I get the previous listindex with mousedown or mouseup etc (not knowing why, it seems not so logical)?

In mousedown the listbox has not yet precessed the click so the list index has not changed because of the click.

If You return true in MouseDown then you are responsible for handling the click… so the listindex won’t change unless you change it.

MouseUp does not fire unless you return true in mousedown so again unless you change it, the listindex will not have been changed.

try putting this in the CELLCLICK event… this should update the listindex
and use it INSTEAD of MouseDown… I have found this to work quite well

  If row>=Me.ListCount Then Return False
  If row<>Me.listindex Then  Me.ListIndex=row

Changes are processed after MouseUp, not MouseDown. That gives the user the chance to cancel his action by moving the mouse off the control before releasing it. This is universal and expected behavior.