I have a treeview with nodes like this: ‘Foo (1234)’, and want to allow the user to rename the nodes, but only the Foo part, without (1234). I first tried to change the node text in BeforeLabelEdit like this:
private void treeView1_BeforeLabelEdit(object sender, NodeLabelEditEventArgs e) { e.Node.Text = 'Foo'; }
But when I click the node to edit it, ‘Foo (1234)’ appears in the textbox.
Okay, then let’s try something else.
I set treeView1.LabelEdit to false, and then do the following:
private void treeView1_MouseClick(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { if (treeView1.SelectedNode == treeView1.GetNodeAt(e.Location)) { treeView1.SelectedNode.Text = 'Foo'; treeView1.LabelEdit = true; treeView1.SelectedNode.BeginEdit(); } } }
And then in AfterLabelEdit, I set LabelEdit back to false.
And guess what? This doesn’t work either. It changes the node text to ‘Foo’ but the edit textbox does not appear.
Any ideas? Thanks
Finally I have found a solution to this on CodeProject. Among the comments at the bottom, you will also find a portable solution.