Dynamic context menu in TreeView nodes

Duplicate Of: Find node by clicking the context menu

I have a context menu in Treeview when the user right-clicks to change based on the current node tag object that was right-clicked.

I am currently updating the context menu in the after_select event, however this does not work when the user right-clicks on another node without selecting it.

How to determine which node right-clicked and changed the context menu? Or am I doing it wrong?

+4
source share
2 answers

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

+4
source
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(eX, eY) 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 
+3
source

All Articles