3

How is it possible to get the selected (clicked on) node in a treeview and return it as a string?

0

2 Answers 2

17
    private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
    {
        string selectedNodeText = e.Node.Text;
    }
Sign up to request clarification or add additional context in comments.

2 Comments

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)
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).
5

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.