afterlabeledit treeview handler c#
i need to based in what the user wrote in the edition of a node label, rewrite that label with other text. Example if the user wrote "NewNodeName" I want that the node text after finish the edition be "S :NewNodeName". I try this two codes and i don't know why neither work
private void treeView1_AfterLabelEdit(object sender, NodeLabelEditEventArgs e)
{
e.Node.Text = "S :"+ e.Label;
开发者_开发技巧}
and also:
private void treeView1_AfterLabelEdit(object sender, NodeLabelEditEventArgs e)
{
treeView1.SelectedNode.Text = "S :"+ e.Label;
}
Yes, doesn't work, the Text property gets the label value after this event runs. Which is why e.Cancel works. So the Text value you assigned will be overwritten again by code that runs after raising this event. Code inside of the native Windows control.
There is no AfterAfterLabelEdit event and you cannot alter e.Label in the event handler, you need a trick. Change the Text property after the event stopped running. Elegantly done by using Control.BeginInvoke(). Like this:
private void treeView1_AfterLabelEdit(object sender, NodeLabelEditEventArgs e) {
this.BeginInvoke((MethodInvoker)delegate { e.Node.Text = "S: " + e.Node.Text; });
}
It's quite late to answer this question, but here's another solution:
1) Remove the part that you want user not to edit of the node label right before you call BeginEdit()
2) In AfterLabelEdit(), set the node text as you want and set NodeLabelEditEventArgs.CancelEdit = true so that the text user input won't replace the text you set
private void treeView1_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
{
if (e.Node == null) return;
e.Node.Text = e.Node.Text.Substring(3, e.Node.Text.Length - 3);
e.Node.BeginEdit();
}
private void treeView1_AfterLabelEdit(object sender, NodeLabelEditEventArgs e)
{
e.Node.Text = "S :" + e.Label;
e.CancelEdit = true;
}
精彩评论