Link to home
Start Free TrialLog in
Avatar of Peter Kiers
Peter KiersFlag for Netherlands

asked on

Edit a treeview-node and then sort the treeview

Hi,

I have found a question and solution on the internet:

Question:
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?

Answer:

Use BeginInvoke:

delegate void sort();

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

I have the same problem but I don't know how to implement it in my
code. I have put the code for editing a treeview node in the code-section.

Who can help me?

Peter
private void MainForm_Load(object sender, EventArgs e)
        {
        tvSampleMenu.MenuItems.Add("Edit", new EventHandler(tvSampleRightClickEdit));
         ....
        }
                //*----------------------------------------------------------*//
        private void tvSampleRightClickEdit(object sender, System.EventArgs e)
        {
                TreeNode node = tvNotes.SelectedNode;
                if (node == null) { return; }
                if (node == tvNotes.Nodes[0]) { return; }
                node.TreeView.LabelEdit = true;
                node.BeginEdit();
            }
          //*----------------------------------------------------------*//
        private void tvNotes_AfterLabelEdit(object sender, NodeLabelEditEventArgs e)
         {
             if (e.Label.Trim().Length < 1) { e.CancelEdit = true; }  
             EditNode(tvNotes.SelectedNode,e.Label);
             tvNotes.SelectedNode.EndEdit(false);
             tvNotes.LabelEdit = false;
         }
         //*----------------------------------------------------------*//
         private void EditNode(TreeNode node, string newText)
        {
            DataRow row = null;
            {
                if (node == null) { return; }
                if (node == tvNotes.Nodes[0]) { return; }
                row = (DataRow)node.Tag;
                if (row == null)
                {
                    return;
                }
                row["Description"] = newText;
            }
            //*----------------------------------------------------------*//

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of SAMIR BHOGAYTA
SAMIR BHOGAYTA
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Peter Kiers

ASKER

Hi, first its VB and second its not the solution.

Peter