Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleDisplay an appropriate popup menu when the user right clicks on a TreeView node in VB .NET
DescriptionThis example shows how to display an appropriate popup menu when the user right clicks on a TreeView node in Visual Basic .NET.
KeywordsTreeView, popup menu, context menu
CategoriesControls, VB.NET, Software Engineering
 
This program makes three types of TreeView nodes representing factories, groups, and persons. It sets each makes an associated object for each node and assigns it to the node's Tag property. It makes a FactoryData object for a factory node, a GroupData object for a group node, and a PersonData object for a person node. These objects don't do much in this example but you can give them whatever data you want.

The trvOrg_MouseDown event handler uses the TreeView control's GetNodeAt method to see which node is under the mouse. It selects that node in the TreeView.

The routine then checks the type of the node's Tag object and displays the appropriate popup menu.

 
Private Sub trvOrg_MouseDown(ByVal sender As Object, ByVal _
    e As System.Windows.Forms.MouseEventArgs) Handles _
    trvOrg.MouseDown
    ' See if this is the right button.
    If e.Button = MouseButtons.Right Then
        ' Select this node.
        Dim node_here As TreeNode = trvOrg.GetNodeAt(e.X, _
            e.Y)
        trvOrg.SelectedNode = node_here

        ' See if we got a node.
        If node_here Is Nothing Then Exit Sub

        ' See what kind of object this is and
        ' display the appropriate popup menu.
        If TypeOf node_here.Tag Is FactoryData Then
            ctxFactory.Show(trvOrg, New Point(e.X, e.Y))
        ElseIf TypeOf node_here.Tag Is GroupData Then
            ctxGroup.Show(trvOrg, New Point(e.X, e.Y))
        ElseIf TypeOf node_here.Tag Is PersonData Then
            ctxPerson.Show(trvOrg, New Point(e.X, e.Y))
        End If
    End If
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated