4

Duplicate Of : Find node clicked under context menu

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?

Community
  • 1
  • 1
dr. evil
  • 26,944
  • 33
  • 131
  • 201

2 Answers2

4

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

Guge
  • 4,569
  • 4
  • 35
  • 47
4
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