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
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.
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.
The TreeNode class represents a node of a TreeView.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With