Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 901
  • Last Modified:

select item on right click in listbox - Access VBA

I have a custom menu that appears when the user right clicks a listbox.   How do I make it so that the item that the mouse is over gets selected before the custom menu comes up?

Thanks!
MikeMc
0
cdmac2
Asked:
cdmac2
  • 2
  • 2
1 Solution
 
harfangCommented:
Hello cdmac2

The default behavior of a shortcut menu (context menu) is to select the item before opening the menu. What sort of menu is it? Do you actually cancel the mouse event?

Anyway, I would have two ideas:

1) use the form timer and instead of showing your menu, set the timer to 1. This would allow the right-click to terminate normally (selecting the line) and your Form_Timer would do the rest...

2) use the metrics of the click and the current position of the scrollbar (requires WinAPI calls) to determine which record *would* have been selected if you had let the click event terminate...

Neither are very attractive...

(°v°)
0
 
cdmac2Author Commented:
Hi Harfang,

Yeah, I would have thought that it would default to select the item before showing the menu, but apparentaly not.  It is a custom shortcut menu I created through Edit -> Toolbars -> Customize.  

To the best of my knowledge, I am not canceling the mouse event.  Here is the code I use to bring up the menu:

Private Sub lstProjID_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
   If Button = 2 Then
      CommandBars.Item("MenuProj").showpopup
   End If
End Sub

Any other thoughts?  I appreciate the ideas above, but like you said, neither are that attractive.

Thanks,
MikeMc
0
 
harfangCommented:
Hello MikeMc

I somehow believed you had created you own shortcut mechanism. If you use standard office menus, you need no VB at all. Delete your event handler, find the property "Shortcut Menu" of lstProjID and select "MenuProj". It will then work as intended.

You will notice that shortcut menus normally appear on mouse up, not mouse down. This allows the default action of the mouse down event (change focus, select in the listbox, etc) to take place. If you have turned off shortcut menus in the form properties, however, you would need the same line of VB, but in the control's MouseUp event.

Cheers, it was easy after all!

(°v°)
0
 
cdmac2Author Commented:
Beautiful... That was it.

Thanks!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now