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
DescriptionThis example shows how to display an appropriate popup menu when the user right clicks on a TreeView node in Visual Basic 6.
KeywordsTreeView, popup menu, context menu
CategoriesControls, Software Engineering
 
In the TreeView's MouseDown event handler, the program uses the control's HitTest method to get the node under the mouse. If the right mouse button is down, the code calls subroutine ShowPopup.

ShowPopup checks the node's type and shows and hides the appropriate context menu items. Alternatively you could use different popup menus for different kinds of nodes. It then uses the PopupMenu statement to display the popup menu.

 
Private Sub OrgTree_MouseDown(Button As Integer, Shift As _
    Integer, x As Single, y As Single)
    Set SourceNode = OrgTree.HitTest(x, y)
    SourceType = NodeType(SourceNode)

    Set OrgTree.SelectedItem = SourceNode

    ' If the right button is down, display the
    ' appropriate context menu.
    If Button = vbRightButton Then ShowPopup
End Sub

' Display the appropriate popup menu items.
Private Sub ShowPopup()
    ' See what kind of node this is and
    ' hide the invalid popup menu items.
    Select Case SourceType
        Case otNone
            ' Allow add factory.
            mnuPopupAddFactory.Visible = True
            mnuPopupAddGroup.Visible = False
            mnuPopupAddPerson.Visible = False

            mnuPopupDeleteSep.Visible = False
            mnuPopupDeleteObject.Visible = False
        Case otFactory
            ' Allow add factory or group.
            mnuPopupAddFactory.Visible = True
            mnuPopupAddGroup.Visible = True
            mnuPopupAddPerson.Visible = False

            mnuPopupDeleteSep.Visible = True
            mnuPopupDeleteObject.Visible = True
            mnuPopupDeleteObject.Caption = "&Delete Factory"
        Case otGroup
            ' Allow add factory, group, or person.
            mnuPopupAddFactory.Visible = True
            mnuPopupAddGroup.Visible = True
            mnuPopupAddPerson.Visible = True

            mnuPopupDeleteSep.Visible = True
            mnuPopupDeleteObject.Visible = True
            mnuPopupDeleteObject.Caption = "&Delete Group"
        Case otPerson
            ' Allow add factory, group, or person.
            mnuPopupAddFactory.Visible = True
            mnuPopupAddGroup.Visible = True
            mnuPopupAddPerson.Visible = True

            mnuPopupDeleteSep.Visible = True
            mnuPopupDeleteObject.Visible = True
            mnuPopupDeleteObject.Caption = "&Delete Person"
    End Select

    ' Display the popup menu.
    PopupMenu mnuPopup
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated