Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Get Node Text of treeView on the click event

I want to Get the Text of the node in the treeview. I am using the click() event.. When I am using the AfterSelect() event I can get the node text by e.Node.text . how can I get the text by using Click() event

like image 411
Jiss Sebastian Avatar asked May 09 '13 08:05

Jiss Sebastian


People also ask

How do I get data from TreeView database?

Populating TreeView from database Inside the PopulateTreeView method, a loop is executed over the DataTable and if the ParentId is 0 i.e. the node is a parent node, a query is executed over the VehicleSubTypes table to populate the corresponding child nodes and again the PopulateTreeView method is called.

How do I know if TreeView node is selected?

SelectedNode; if ( tn == null ) Console. WriteLine("No tree node selected."); else Console. WriteLine("Selected tree node {0}.", tn.Name ); You can compare the returned TreeNode reference to the TreeNode you're looking for, and so check if it's currently selected.

Which property in the TreeView control represent the node?

The TreeNode class represents a node of a TreeView.


1 Answers

I don't recommend using the Click event for this. The reason is that there are a lot of different places that the user could click on a TreeView control, and many of them do not correspond to an actual node. The AfterSelect event is a much better choice—it was designed for this usage.

Beyond that, the Click event is rather difficult to use, because it doesn't provide you very much information in the handler method. It doesn't tell you which button was clicked, where the click event occurred, etc. You have to retrieve all of this information manually. It's recommended that you subscribe to either the MouseClick or the MouseDown/MouseUp event instead.

To figure out what the user clicked on, you need to use the TreeView.HitTest method, which returns a TreeViewHitTestInfo object that contains detailed information about the area where the user clicked, or the somewhat simpler TreeView.GetNodeAt method, which will simply return null if no node exists at the location of the click.

Alternatively, to get the currently selected node at any time, you can just query the TreeView.SelectedNode property. If no node is selected, this will also return null.

like image 95
Cody Gray Avatar answered Oct 13 '22 00:10

Cody Gray