[Webinar] Streamline your web hosting managementRegister Today


Detecting Right Mouse Button Event

Posted on 2000-02-03
Medium Priority
Last Modified: 2008-02-20
I have a VSFlexGrid which when the user clicks the right mouse button brings up a pop-up menu.  I need to know what column the user was in when they performed the mouse click but the right mouse button event does not cause a click event in the grid.  I need to know either a) how to generate the event by using the right mouse button or b) simulate a left mouse button click to fire up the event.
Question by:JEGB

Expert Comment

ID: 2485058
if there is a mouse down event then there is a variable passed that tells you which button has been pressed.

Expert Comment

ID: 2485293
Sub Grid1_MouseDown (Button As Integer, X As Single, Y As Single..)

   If (Button = vbRightButton) Then
      'Show the popupmenu here
   End If

End Sub

Author Comment

ID: 2485915
To clarify:  If you click the Left Mouse button in a VSFlexGrid the Row and Col properties change to the new cell (row and col) properties that the mouse cursor is in when the event occurred.  However if you click the right mouse button in a cell you can detect the event BUT the col and row properties do not change but instead remain what they were before the event.  I need to know a way of detecting what column the mouse is in when the right mouse button event occurs.

Author Comment

ID: 2485951
I have this Q answered on another Question I've posted, basically you use the MouseRow and MouseCol properties, which I must have missed while scanning the help.

Accepted Solution

pmwood earned 500 total points
ID: 2486018
If the grid does not support a right-click event, you will have to subclass the control window using the Windows API to know when it happens.

Use the SetWindowLong(lhWnd, GWL_WNDPROC, AddressOf WindProc)API call with VSgrid.hWnd property to redirect window messages to a custom procedure, WindProc.

Within WindProc, trap the WM_RBUTTONDOWN message and call a procedure to do what you want on the right-button-click.  You MUST PASS all other window messages to the default window event handler.

Set the message handler back to the default BEFORE unloading the form with the control on it.

Examples of the use of SetWindowLong/WindProc can be found on MSDN or the MS web site knowledge base - to much to include here, but you will need them.

NB.  The VB IDE is v. sensitive to this API, and may crash during debugging.  I use a conditional compilation argument to avoid this when working on other parts of my code.

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

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

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses
Course of the Month9 days, 21 hours left to enroll

591 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