Reputation: 1526
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
Upvotes: 13
Views: 11407
Reputation: 1526
Finally I have found a solution to this on CodeProject. Among the comments at the bottom, you will also find a portable solution.
Upvotes: 5
Reputation: 204219
Heh - I struck that one a few years back. I even left a suggestion on Connect (vote for it!) to allow the label to be changed in BeforeLabelEdit.
One option (in WinForms - it's a different story in WPF) is to use custom painting for your TreeNodes so that the actual label is still "Foo" and you custom draw the " (1234)" after it. It's a bit of a pain to get right though.
Upvotes: 4