Solved

Right click on TreeView Node

Posted on 1999-01-12
3
402 Views
Last Modified: 2010-05-03
How do I to get a right click on a treeview node to behave like a left click in that it will highlight the node selected by the right click action?  

Upon a right click I display a popup menu. Currently if I right click on a node which is not currently highlighted, it will display the menu for the node that is highlighted, and not the one I am clicking.

This would seem a reasonable thing to do as this is how it currently works in Windows explorer.  ie) if you click on  an item, then right click on a different item, the new item you right clicked will be highlighted, and the popup menu will be displayed for the new item.
0
Comment
Question by:wfinn
3 Comments
 
LVL 8

Accepted Solution

by:
MikeP090797 earned 100 total points
Comment Utility
Try this:
in TreeView1_MouseDown:
IF Button=2 then Set Treeview1.SelectedItem = TreeView.HitTest(X, Y).
'Display menu here
0
 

Author Comment

by:wfinn
Comment Utility
The proposed answer is good.  However, it still behaves differently than in Windows Explorer.  That is, if I right click and the menu appears, I can not right click on another node.  However, in Explorer, I can right click on a node, then immediately right click on another.  If I select the wrong node with the right click and then decide I need to select another node, I have to left on something else first, then right click.
0
 
LVL 1

Expert Comment

by:twolff
Comment Utility
Here is something that I worked up. This should help you:
    With TreeView1
        Set .SelectedItem = .HitTest(x, y)
    End With
    If Button = 2 Then
        PopupMenu mnu1, vbPopupMenuRightButton, x, y
    End If

The PopupMenu procedure only works with the left button by default. If you include the flag vbPopupMenuRightButton it will also work with the right button. This will allow the mousedown to event to fire on the treeview and will automatically hide the popup menu. Enjoy!
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

744 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

12 Experts available now in Live!

Get 1:1 Help Now