How is it possible to get the selected (clicked on) node in a treeview and return it as a string?
2 Answers
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
string selectedNodeText = e.Node.Text;
}
2 Comments
Cody Gray
Do note that you can obtain the selected node at any time using the
TreeView.SelectedNode property. You don't have to do it from within an event handler method. Something like: MessageBox.Show(myTreeView.SelectedNode.Text)ken2k
You are right, I assumed the OP wanted to get the name from the 'selection changed' event (because of the first comment of the OP).
From the docs:
http://msdn.microsoft.com/en-us/library/system.windows.forms.treenode.aspx
Maybe:
MessageBox.Show(((TreeView)sender).SelectedNode.Text)
Or
MessageBox.Show(((TreeView)sender).SelectedNode.Name)