.comment-link {margin-left:.6em;}

kHSw

Thursday, January 12, 2006

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

6 Comments:

  • Thanx , it helps me a lot .
    I've had some troubles until I find ur solution.

    By Anonymous lonelyghost, at 7:18 AM  

  • You're welcome!

    By Blogger kHSw, at 11:00 PM  

  • This absolutely saved my life. I don't know why MS chose to implement SelectedNode this way, with so little documentation into its behaviour astounds me. This ended days of searching. I thank you greatly!

    By Anonymous Daryl Johnson, at 11:40 PM  

  • Thank you! I really appreciate your comment. It's always nice to know that my code is useful to other people.

    By Blogger kHSw, at 5:50 PM  

  • Thanks, your code saved me a LOT of time!

    By Anonymous Anonymous, at 11:43 PM  

  • Thank you for your comment!

    By Blogger kHSw, at 11:33 AM  

Post a Comment

Links to this post:

Create a Link

<< Home


 
Stefanie Worm is het liefste vrouwtje van de wereld.
Melina is de liefste schatsie van de wereld (Erik De Maeyer).