C# Windows Form TreeView Sort after LabelEdit

csortingtreeviewwinforms

After a node's label is edited in the tree I try to resort the nodes to place the updated item in the right position. I do this by calling .Sort in AfterLabelEdit event handler which causes an infinite loop.

How can I resort the nodes in a treeview after a label has been changed?

Best Answer

Use BeginInvoke:

    delegate void sort();

    private void treeView1_AfterLabelEdit(object sender, NodeLabelEditEventArgs e)
    {
        treeView1.BeginInvoke(new sort(treeView1.Sort));
    }