Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
    }
like image 808
mjsr Avatar asked Feb 25 '23 11:02

mjsr


1 Answers

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; });
    }
like image 101
Hans Passant Avatar answered Feb 28 '23 02:02

Hans Passant