views:

4044

answers:

2

Duplicate Of : http://stackoverflow.com/questions/2527/c-treeview-context-menus

I've got a context menu on a Treeview, when the user right clicks it supposed to change based on the currently right clicked node's tag object.

Currently I'm updating the context menu in after_select event, however this doesn't work when user right clicks to another node without selecting it.

How can I detect which node right clicked and change the context menu? Or am I doing it wrong?

+2  A: 

You can use the MouseDown event and the HitTest method to find out which node was clicked.

Guge
that worked nicely, thanks.
dr. evil
My pleasure. Thanks for the accept.
Guge
+1  A: 
Private Sub tvTables_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles tvTables.MouseDown
    If e.Button = Windows.Forms.MouseButtons.Right Then
        Dim M As New ContextMenuStrip
        Dim HTI As TreeViewHitTestInfo = tvTables.HitTest(e.X, e.Y)
        If HTI.Node.Level = 0 Then
            M = T1Menu
        ElseIf HTI.Node.Level = 1 Then
            M = T2Menu
        ElseIf HTI.Node.Level = 2 Then
            M = T3Menu
        End If
        tvTables.ContextMenuStrip = M
        tvTables.ContextMenuStrip.Show()
    End If
End Sub