Solved

select item on right click in listbox  - Access VBA

Posted on 2006-06-09
4
779 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

Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

856 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