I have a tree view with items which each have a ContextMenuStrip. How do i know on which tree node the user activated the context menu when handling events like clicks on items in the context menu?
Printable View
I have a tree view with items which each have a ContextMenuStrip. How do i know on which tree node the user activated the context menu when handling events like clicks on items in the context menu?
I was intrigued by your question so I had a wee look. The SourceControl property of the menu is no use because that refers to the TreeView containing the node. I don't know if there's a better way but this is what I came up with:vb Code:
Private Sub TreeView1_MouseDown(ByVal sender As Object, _ ByVal e As System.Windows.Forms.MouseEventArgs) Handles TreeView1.MouseDown If e.Button = Windows.Forms.MouseButtons.Right Then Me.ContextMenuStrip1.Tag = Me.TreeView1.GetNodeAt(e.Location) End If End Sub Private Sub Item1ToolStripMenuItem_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Item1ToolStripMenuItem.Click Dim sourceNode As TreeNode = TryCast(Me.ContextMenuStrip1.Tag, TreeNode) If sourceNode IsNot Nothing Then MessageBox.Show("Source node: " & sourceNode.Text) End If End Sub