Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Visual Basic
Expertise: Intermediate
Aug 1, 1997

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't explain how to trap the right mouse button in a node. The Treeview_MouseDown event occurs before the NodeClick event. In order to display context menus over a node, use this code and define the Key for each node 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 Boolean

Private Sub Form_Load()
        bRightMouseDown = False
End Sub

Private 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 If
End Sub

Private Sub treeview1_MouseUp_
        (Button As Integer, Shift As _
        Integer, X As Single, Y As Single)
                bRightMouseDown = False
End Sub

Private 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 Select
End Sub
Victor Raposo
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date