Determining which node in a Treeview has been clicked

Is the "AfterSelected" event the best place to respond to a click on a node?

How do I determine which node has been clicked? The following code does not work, b ut rather tells me, "The name 'NodeBetter' does not exist in the current context"

private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (e.Node.Name == NodeBetter)
            {

            }
            // else NodeUh, NodeOze
        }

Upvotes: 3

Views: 446

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1500575

It's not clear what you expected NodeBetter to refer to, but TreeViewEventArgs.Node is indeed what you want. As per the documentation:

Gets the tree node that has been checked, expanded, collapsed, or selected.

Perhaps you meant:

if (e.Node.Name == "NodeBetter")

?

It's not clear whether you really want the Selected event though. Would you want your handler to be called if the node were expanded but not selected, for example?

Upvotes: 3

Related Questions