SelectedNode in a TreeView
If you use the default TreeView in Visual Studio 2003, the SelectedNode property doesn't always return the correct selected node in the Click event.
If you inherit from the TreeView, you can override the OnMouseDown event with this code:
Protected Overrides Sub OnMouseDown(ByVal e As System.Windows.Forms.MouseEventArgs)
Me.SelectedNode = Me.GetNodeAt(New Point(e.X, e.Y))
MyBase.OnMouseDown(e)
End Sub
Otherwise, you can add this code to your form in the OnClick event of the Treeview:
Private Sub TreeView1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TreeView1.MouseDown
TreeView1.SelectedNode = TreeView1.GetNodeAt(New Point(e.X, e.Y))
End Sub
If you inherit from the TreeView, you can override the OnMouseDown event with this code:
Protected Overrides Sub OnMouseDown(ByVal e As System.Windows.Forms.MouseEventArgs)
Me.SelectedNode = Me.GetNodeAt(New Point(e.X, e.Y))
MyBase.OnMouseDown(e)
End Sub
Otherwise, you can add this code to your form in the OnClick event of the Treeview:
Private Sub TreeView1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TreeView1.MouseDown
TreeView1.SelectedNode = TreeView1.GetNodeAt(New Point(e.X, e.Y))
End Sub