Solved

select item on right click in listbox  - Access VBA

Posted on 2006-06-09
4
722 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Beautiful... That was it.

Thanks!
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

In the previous article, Using a Critera Form to Filter Records (http://www.experts-exchange.com/A_6069.html), the form was basically a data container storing user input, which queries and other database objects could read. The form had to remain op…
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 utilizing SQL Server functions 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 Ac…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.

772 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