Afterlabeledit C # tree handler

I need to rely on what the user wrote in the edition of the node label, to rewrite this label with other text. Example, if the user wrote "NewNodeName", I want the text node to complete the release of "S: NewNodeName". I try these two codes and I don't know why not a single job

private void treeView1_AfterLabelEdit(object sender, NodeLabelEditEventArgs e) { e.Node.Text = "S :"+ e.Label; } 

and:

  private void treeView1_AfterLabelEdit(object sender, NodeLabelEditEventArgs e) { treeView1.SelectedNode.Text = "S :"+ e.Label; } 
+4
source share
2 answers

Yes, it does not work, the Text property gets the label value after this event. That is why e.Cancel works. Thus, the Text value that you have assigned will be overwritten again by the code that is fired after raising this event. Code inside a native Windows control.

There is no AfterAfterLabelEdit event and you cannot change e.Label in the event handler, you need a trick. Change the Text property after the event stops. Elegantly done using Control.BeginInvoke (). Like this:

  private void treeView1_AfterLabelEdit(object sender, NodeLabelEditEventArgs e) { this.BeginInvoke((MethodInvoker)delegate { e.Node.Text = "S: " + e.Node.Text; }); } 
+6
source

It is very late to answer this question, but here is another solution:

1) Remove the part you want the user to not edit the node label right before calling BeginEdit ()

2) In AfterLabelEdit () set the text node as you want and set NodeLabelEditEventArgs.CancelEdit = true so that the text user input does not replace the text that 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; } 
+2
source

Source: https://habr.com/ru/post/1333832/


All Articles