Listbox Click and MouseUp events not returning expected results

D

Dale Fye

I've got a listbox (Multiselect = None) on a form (mdb database in Access
2007 with SP1).

When I right click on an item in the list, I popup a shortcut menu that
allows me to call a function that is supposed to open a report for the item
that I just "clicked" on. I'm using the MouseUp event to check for the
right button click. Unfortunately, it is not working the way I expected.

If I click (left button) on a list item, the lst_MouseUp event fires,
followed by the lst_Click event, and when I test the value of the list box,
it is correct. This is great. And if I follow this up with a RightClick, I
get the report for the correct item from the listbox.

However, if I RightClick on a previously unselected item from the list, and
run the report, I will normally get the report for the item that was last
"selected" via LeftClick. I had expected this might happen with the
MouseDown event, that's why I selected the MouseUp event.

When I added some breakpoints to my code, to see what was happening, things
got wierd. Although the Access help for the Listbox.Click event says that
"clicking a control with the right or middle mouse button does not trigger
this event", my code executes the Listbox.Click event, and does not execute
the MouseUp event. However, if I remove the breakpoints from the Click and
MouseUp events, then I get the following:

If I LeftClick an item, the Click event fires. If I follow that up with a
RightClick on the same list item, then the MouseUp event fires, and the
value of the list box is that of the item selected.

If I LeftClick an item, the Click event first. If I follow that up by
RightClicking on a different item, MouseUp event fires, and the value and
listindex of the listbox are the value and listindex associated with the
item that was selected via the previous LeftClick.

If I select an item via a RightClick, and then RightClick on another item,
then the value and listindex of the listbox will be the ones associated with
the most recent RightClick.

What I need is a foolproof way to ensure that no matter whether the user
LeftClicks, then RightClicks, or just RightClicks on a list item, the
"value" of the list will be that associated with the RightClick. I've tried
using the Listindex as well as checking to see whether the Selected property
of each of the list items could be used, but neither of these return the
guaranteed results I'm expecting.

Any ideas.

Dale
 
D

Dirk Goldgar

Dale Fye said:
I've got a listbox (Multiselect = None) on a form (mdb database in Access
2007 with SP1).

When I right click on an item in the list, I popup a shortcut menu that
allows me to call a function that is supposed to open a report for the
item that I just "clicked" on. I'm using the MouseUp event to check for
the right button click. Unfortunately, it is not working the way I
expected.

If I click (left button) on a list item, the lst_MouseUp event fires,
followed by the lst_Click event, and when I test the value of the list
box, it is correct. This is great. And if I follow this up with a
RightClick, I get the report for the correct item from the listbox.

However, if I RightClick on a previously unselected item from the list,
and run the report, I will normally get the report for the item that was
last "selected" via LeftClick. I had expected this might happen with the
MouseDown event, that's why I selected the MouseUp event.

When I added some breakpoints to my code, to see what was happening,
things got wierd. Although the Access help for the Listbox.Click event
says that "clicking a control with the right or middle mouse button does
not trigger this event", my code executes the Listbox.Click event, and
does not execute the MouseUp event. However, if I remove the breakpoints
from the Click and MouseUp events, then I get the following:

If I LeftClick an item, the Click event fires. If I follow that up with a
RightClick on the same list item, then the MouseUp event fires, and the
value of the list box is that of the item selected.

If I LeftClick an item, the Click event first. If I follow that up by
RightClicking on a different item, MouseUp event fires, and the value and
listindex of the listbox are the value and listindex associated with the
item that was selected via the previous LeftClick.

If I select an item via a RightClick, and then RightClick on another item,
then the value and listindex of the listbox will be the ones associated
with the most recent RightClick.

What I need is a foolproof way to ensure that no matter whether the user
LeftClicks, then RightClicks, or just RightClicks on a list item, the
"value" of the list will be that associated with the RightClick. I've
tried using the Listindex as well as checking to see whether the Selected
property of each of the list items could be used, but neither of these
return the guaranteed results I'm expecting.

Any ideas.


Interesting, Dale. So far as I can see, the behavior has changed between
Access 2003 and 2007. When I use the mouse on a nonmultiselect list box in
each version, I get these sequences of events:

Access 2003
------------------
Left-Click: MouseDown --> MouseUp --> Click
Right-Click: MouseDown --> Click --> MouseUp

Access 2007
------------------
Left-Click: MouseDown --> MouseUp --> Click
Right-Click: MouseDown --> MouseUp --> Click

In both versions, in my test, the ListIndex and Value properties of the list
box weren't updated to reflect the row I clicked on until the Click event.
That meant that in Access 2003, because of it's odd event sequence,
right-clicking on the list box made the new row "available" in the MouseUp
event, but not when left-clicking. And in Access 2007, the new row was
never available in the MouseUp event.

I can't say to what extent these results are timing-dependent, but they do
suggest that you can't trust the ListIndex or Value until the Click event
fires. At least, though, the Click event does fire when right-clicking. If
you can rely on that, how about capturing which mouse button was clicked in
the MouseUp event, and then checking that value in the Click event, so that
you can pop up your menu and act on the now-available row information?

That implies that your "popup menu" must really be a form that you open, not
a true shortcut menu. And you'd need to disable any built-in shortcut menu,
so it wouldn't interfere with your own form or be confusing.

The only alternative to this that I can think of would be to use the mouse
coordinates in the MouseUp event to calculate which row the mouse is over.
That should be possible, and it's the sort of thing Stephen Lebans may have
done, but I don't know how to do it now.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top