Solved

select item on right click in listbox  - Access VBA

Posted on 2006-06-09
4
766 Views
Last Modified: 2008-01-09
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
Comment
Question by:cdmac2
  • 2
  • 2
4 Comments
 
LVL 58

Expert Comment

by:harfang
ID: 16875386
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
 
LVL 1

Author Comment

by:cdmac2
ID: 16875417
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
 
LVL 58

Accepted Solution

by:
harfang earned 500 total points
ID: 16876839
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
 
LVL 1

Author Comment

by:cdmac2
ID: 16877067
Beautiful... That was it.

Thanks!
0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now