Trap Right-Clicks on TreeView’s Nodes

The TreeView control gives your apps a good Windows 95 look and feel. However, the VB manual doesn’texplain how to trap the right mouse button in a node. The Treeview_MouseDown event occurs before theNodeClick event. In order to display context menus over a node, use this code and define the Key for eachnode with a letter followed by a number:

 + Root (R01)                    'the letter gives |--- Child 1 (C01)      'the indication to|--+ Child 2 (C02)      'the context menu|  |--- Child 2.1 (H01)|  |--- Child 2.2 (H02)Dim bRightMouseDown as BooleanPrivate Sub Form_Load()        bRightMouseDown = FalseEnd SubPrivate Sub treeview1_MouseDown_        (Button As Integer, Shift As _        Integer, X As Single, Y As Single)        If Button And vbRightButton Then                bRightMouseDown = True        Else                bRightMouseDown = False        End IfEnd SubPrivate Sub treeview1_MouseUp_        (Button As Integer, Shift As _        Integer, X As Single, Y As Single)                bRightMouseDown = FalseEnd SubPrivate Sub treeview1_NodeClick_        (ByVal Node As Node)        Select Case Left(Node.Key, 1)                Case "R"                        If Not bRightMouseDown Then                                'do the normal node click,                                 'so you must here the code                                 'for the node code click                        Else                                'select the node                                treeview1.Nodes(Node.Key).Selected _                = True                                'show the popup menu                                PopupMenu mnuContext1                End If                 Case "C"                        If Not bRightMouseDown Then                                'do the normal node click,                                 'so you must here the code                                 'for the node code click                        Else                                'select the node                                treeview1.Nodes(Node.Key).Selected _                = True                                'show the popup menu                                PopupMenu mnuContext2                End If                ' and so on with all other nodes                ' ....        End SelectEnd Sub
Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

Recent Articles: