Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Treeview with right-click menu?

Status
Not open for further replies.

norwood

Technical User
Feb 24, 2003
14
0
0
US
Does anyone know a way of adding a right-click menu for a treeview control?

I'd like to give the users some options when right-clicking items in my treeview, such as "open form", "delete item", etc.

Is this possible?

any help is appreciated!!!!
 
Yes it is possible. You need to search this (and other) forums for examples.
 
Thanks!

I've tried searching this forum but didn't find any post that would contain specifically that... maybe I searched for the wrong terms? If you could point me to any thread (here or somewhere else), it would really help.

:)
 
First off, create custom commandbars (I.e. shortcut menus) that you want to use for your treeview. Then here is a snippet of code that should get you on your way.
Code:
Private Sub treeView_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Long, ByVal Y As Long)
On Error GoTo Err_treeView_MouseDown

    Dim nodOverNode As Node
    
    Set nodOverNode = Me.treeView.HitTest(X, Y)
    
    If Not nodOverNode Is Nothing Then
        Me.treeView.Nodes(nodOverNode.index).Selected = True
        Call treeView_NodeClick(nodOverNode)
    End If
    
    If Button = acRightButton Then ' Popup shortcut menu, but which one?
        If nodOverNode Is Nothing Then ' This one is not context sensitive, it is the form default.
            Application.CommandBars(Me.ShortcutMenuBar).ShowPopup
        Else ' This one is context sensitive (I.e. could be different depending on node clicked)
            Application.CommandBars("MyShortcutMenuBar").ShowPopup ' where MyShortcutMenuBar is your custom menubar name
        End If
    End If

Exit_treeView_MouseDown:
    Exit Sub

Err_treeView_MouseDown:
    MsgBox Err.Description
    Resume Exit_treeView_MouseDown

End Sub
 
I'm getting an error at
Call treeView_NodeClick(nodOverNode)
saying "sub or function not defined". Any idea why is that?

BTW, I've changed the calls to treeview to my own treeview name (axTreeview).

thanks!
 
Given that you are using axTreeview, the event procedure you may want to call is axTreeview_NodeClick. If you do not want anything happening when the user clicks on the node then you can remove this bit of code. Otherwise you will need to create that procedure (if it does not already exist) and write the appropriate code.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top