08/06/2014 by Nitesh

How To Get Node at a Given Point in Tree View

Friends,

Today, I was working with Treeview Control and in the MouseMove event handler of the control, I was supposed to get the node of the TreeView control present at the current mouse pointer. I looked around for sometime before I figured out how this can be done. In this post, we will see how we can achieve the same. To do this, we will use the GetNodeAt() function that will let us know the Node of the TreeView control present at the current mouse position.

      Point pt = new Point(e.X, e.Y);
      TreeNode DestinationNode = treeView1.GetNodeAt(pt);

In the 1st line of code, we get the mouse co-ordinates using the e.X and e.Y positions in the MouseMove event handler and convert it into a Point object. In the second line, we use the GetNodeAt() function of the TreeView control to return the node present at the specified point. If not node is present, NULL is returned.

Hope this tip helps you! Keep learning and sharing! Cheers!

#.Net#TreeView#Winforms