Hello all,
I am trying to make a ContextMenu appear on right-click in a TreeView, but only when the user right-clicks on a certain "level" of nodes. I actually have this working. The problem is that the ContextMenu does not disappear on the first click when the TreeView is clicked somewhere else after the ContextMenu appears. The menu disappears on a second click, but I need for it to disappear on the first (i.e., normal program behavior with which we are all familiar).
Here's my code so far.
Don't worry about the GetNodeNestingLevel function - it is working properly.
Any ideas?
Thanks,
JEB
I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson
I am trying to make a ContextMenu appear on right-click in a TreeView, but only when the user right-clicks on a certain "level" of nodes. I actually have this working. The problem is that the ContextMenu does not disappear on the first click when the TreeView is clicked somewhere else after the ContextMenu appears. The menu disappears on a second click, but I need for it to disappear on the first (i.e., normal program behavior with which we are all familiar).
Here's my code so far.
Code:
Private Sub TreeView1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TreeView1.MouseDown
Dim dp As System.Drawing.Point
TreeView1.ContextMenu = Nothing
If e.Button = MouseButtons.Right Then
'select node that is being right-clicked
TreeView1.SelectedNode = TreeView1.GetNodeAt(e.X, e.Y)
'Get the nesting level of the selected node
NodeLevel = GetNodeNestingLevel(TreeView1.SelectedNode)
'only display context menu on lowest-level nodes
If NodeLevel = 3 Then
TreeView1.ContextMenu = TVMenu
dp = New System.Drawing.Point(e.X, e.Y)
TreeView1.ContextMenu.Show(TreeView1, dp)
Else
TreeView1.ContextMenu = Nothing
End If
ElseIf e.Button = MouseButtons.Left Then
TreeView1.SelectedNode = TreeView1.GetNodeAt(e.X, e.Y)
TreeView1.ContextMenu = Nothing
End If
NodeLevel = -1
End Sub
Don't worry about the GetNodeNestingLevel function - it is working properly.
Any ideas?
Thanks,
JEB
I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson