TreeNode not selected after leaving textbox with messagebox, C#

195 views Asked by At

I have a WindowsForm with a treeview and a textbox. When the textbox looses focus a messagebox is shown to ask if the text in the textbox is to be saved. No problem there. But I want the focus to then be on the treeview and the node that was clicked. I can get the treeview selected, but not the node.

1

There are 1 answers

0
Stefan Wanitzek On BEST ANSWER

You have to call

myTreeView.Select();

to make sure that the node will be selected.

Full example:

// Add some example nodes
treeView1.Nodes.Add("Node 1");
treeView1.Nodes.Add("Node 2");
treeView1.Nodes.Add("Node 3");

// Change selection
treeView1.SelectedNode = treeView1.Nodes[1];

// Neccessary to highlight the selected node:
treeView1.Select();