Live Chat Icon For mobile
Live Chat Icon

When I get the SelectedNode in the treeview’s Click event, it is the old selection. How do I get the newly selected node

Platform: WinForms| Category: TreeView

Try using the AfterSelect event instead of the Click event. The Click event is inherited from Control.Click and occurs before the new selection is set into SelectedNode. The AfterSelect event is fired after the newly selected node is placed in the SelectedNode property. This code illustrates these events.

	
private void treeView2_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)
{
	TreeNode node = treeView2.SelectedNode;
	Console.WriteLine('AfterSelect:' + node.ToString());//from tree
	Console.WriteLine('AfterSelect:' + e.Node.ToString());//from event args

}

private void treeView2_Click(object sender, System.EventArgs e)
{
	TreeNode node = treeView2.SelectedNode;
	if(node == null)
		Console.WriteLine('Click: (none)');
	else
		Console.WriteLine('Click: ' + node.ToString());
}

Share with

Share on twitter
Share on facebook
Share on linkedin

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.