Solved

select item on right click in listbox  - Access VBA

Posted on 2006-06-09
4
833 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Does Your Cloud Backup Use Blockchain Technology?

Blockchain technology has already revolutionized finance thanks to Bitcoin. Now it's disrupting other areas, including the realm of data protection. Learn how blockchain is now being used to authenticate backup files and keep them safe from hackers.

Question has a verified solution.

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

Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

635 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