[RESOLVED] [2005] Cancel Contextmenu in Treeview
I only want to dispaly a context menu in the treeview if a node is selected.
This is what I'm trying but it doesn't seem to work. The menu apprears if I right click in the treeview whether I'm on a node or not and will not go away until l click somewhere else.
vb Code:
Private Sub tvwCharts_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles tvwCharts.MouseDown
If e.Button = Windows.Forms.MouseButtons.Right Then
Me.ContextMenuStrip1.Tag = tvwCharts.GetNodeAt(e.Location)
If ContextMenuStrip1.Tag Is Nothing Then
ContextMenuStrip1.Visible = False
End If
End If
End Sub
Re: [2005] Cancel Contextmenu in Treeview
1. Set the treeview's contextmenustrip property to nothing (that is to remove ContextMenuStrip1 from your treeview contextmenustrip property)
2. Use nodemouseclick event of the node
Code:
Private Sub TreeView1_NodeMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeNodeMouseClickEventArgs) Handles TreeView1.NodeMouseClick
If e.Button = Windows.Forms.MouseButtons.Right Then
'Optional: you can also select the node
TreeView1.SelectedNode = e.Node
ContextMenuStrip1.Visible = True
ContextMenuStrip1.Location = TreeView1.PointToScreen(e.Location)
End If
End Sub
Re: [2005] Cancel Contextmenu in Treeview
Use the "NodeMouseClick" event.
Edit** as shown above.
Re: [2005] Cancel Contextmenu in Treeview
Ah, so you mean that I use the right tool for the job.
Thanks
Re: [2005] Cancel Contextmenu in Treeview
I just implemented your suggestions and it did exactly what I want. Thanks again.