Righ Mouse in TreeView

  • Thread starter Thread starter Brian P. Hammer
  • Start date Start date
B

Brian P. Hammer

How do I determine what node was clicked with the right mouse button? I have a context menu that pops open and have delete as an option. Unless the user first selects the node, the node that was last left clicked will be deleted and not the one the user right clicked one. Any example would be great.

It seems odd that this would not be a part of the tree collection?
 
I am using VB but C# would be fine if it can be easily converted.

I saw this posted by Eric (In C#) but I cannot seem to figure out how to use it...

Private Sub treAircraft_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
If e.Button = MouseButtons.Right Then
Dim tn As TreeNode = treAircraft.GetNodeAt(e.X, e.Y)
If tn Is Nothing Then
Return
End If
If tn.Index = 1 Then
treAircraft.ContextMenu = Nothing
Else
treAircraft.SelectedNode = tn
treAircraft.ContextMenu = cmuAircraftTree
MsgBox(Me.treAircraft.SelectedNode.Text)
End If
End If
End Sub
 
You know you have been looking at something too long when you don't even
notice there isn't a handle for the event. Thanks for the help

Private Sub myTreeView_MouseDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles treAircraft.MouseDown

If e.Button = MouseButtons.Right Then

Dim node As TreeNode = treAircraft.GetNodeAt(e.X, e.Y)

If node Is Nothing Then

Return

End If

treAircraft.SelectedNode = node

End If

End Sub
 
Back
Top