locked
Replace a TreeNode in a TreeView control RRS feed

  • Question

  • Hi, is it possible to replace a specific Treenode with another? and if so how?
    Tuesday, August 5, 2008 6:06 PM

Answers

  • Hi Maclol,

    To replace a specified TreeNode, perhaps you can take a look at the following code snippet.

    In the code snippet, “FindNode” method try to find a specified node (“searchNode”) and replace it with “replacedWith”  from the “rootNode” and all the child nodes.

            private void button1_Click(object sender, EventArgs e)

            {

                      

                TreeNode t2 = new TreeNode("new text");

                FindNode(treeView1.Nodes[0], treeView1.Nodes[0].Nodes[1].Nodes[0],t2);

             

            }

     private void FindNode(TreeNode rootNode,TreeNode searchNode,TreeNode replacedWith)

            {

                foreach (TreeNode tn1 in rootNode.Nodes)

                {

                    if (tn1.Equals(searchNode))

                        tn1.Text = replacedWith.Text;

     

                    FindNode(tn1, searchNode, replacedWith);

                }

     

               

            }

     

    Regards,

    Xun


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    • Edited by jack 321 Friday, August 8, 2008 9:34 AM edit code
    • Marked as answer by jack 321 Monday, August 11, 2008 11:43 AM
    Friday, August 8, 2008 9:19 AM
  • Xun Ye has replied to your query. In that solution you are able to rplace the Text of the node. If you want to replace entire node (existing in treeView1) with another node then use below code instead of 'foreach' block

            private void FindNode(TreeNode rootNode,TreeNode searchNode,TreeNode replacedWith)

            {

                     for (int i = 0; i < rootNode.Nodes.Count; i++) {

                    if (rootNode.Nodes[i].Equals(searchNode))

                        rootNode.Nodes[i] = replacedWith;

                    FindNode(rootNode.Nodes[i], searchNode, replacedWith);
                }

            }


    just modified Xun Ye solution


    Wife is like a Software, Lots of bugs.
    • Marked as answer by jack 321 Monday, August 11, 2008 11:43 AM
    Friday, August 8, 2008 11:21 AM

All replies

  • Sure is!  How is your tree setup, are you dynamicly adding nodes in the expand event?  Is it just a bunch of static nodes?
    Tuesday, August 5, 2008 7:44 PM
  • Hi Maclol,

    To replace a specified TreeNode, perhaps you can take a look at the following code snippet.

    In the code snippet, “FindNode” method try to find a specified node (“searchNode”) and replace it with “replacedWith”  from the “rootNode” and all the child nodes.

            private void button1_Click(object sender, EventArgs e)

            {

                      

                TreeNode t2 = new TreeNode("new text");

                FindNode(treeView1.Nodes[0], treeView1.Nodes[0].Nodes[1].Nodes[0],t2);

             

            }

     private void FindNode(TreeNode rootNode,TreeNode searchNode,TreeNode replacedWith)

            {

                foreach (TreeNode tn1 in rootNode.Nodes)

                {

                    if (tn1.Equals(searchNode))

                        tn1.Text = replacedWith.Text;

     

                    FindNode(tn1, searchNode, replacedWith);

                }

     

               

            }

     

    Regards,

    Xun


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    • Edited by jack 321 Friday, August 8, 2008 9:34 AM edit code
    • Marked as answer by jack 321 Monday, August 11, 2008 11:43 AM
    Friday, August 8, 2008 9:19 AM
  • Xun Ye has replied to your query. In that solution you are able to rplace the Text of the node. If you want to replace entire node (existing in treeView1) with another node then use below code instead of 'foreach' block

            private void FindNode(TreeNode rootNode,TreeNode searchNode,TreeNode replacedWith)

            {

                     for (int i = 0; i < rootNode.Nodes.Count; i++) {

                    if (rootNode.Nodes[i].Equals(searchNode))

                        rootNode.Nodes[i] = replacedWith;

                    FindNode(rootNode.Nodes[i], searchNode, replacedWith);
                }

            }


    just modified Xun Ye solution


    Wife is like a Software, Lots of bugs.
    • Marked as answer by jack 321 Monday, August 11, 2008 11:43 AM
    Friday, August 8, 2008 11:21 AM
  • You can also just remove the node you want to replace, get the index of that node and then use the Insert() method.

    public void ReplaceNode(TreeNode rootNode, TreeNode oldNode, TreeNode newNode)
    {    
        int
    index = rootNode.Nodes.IndexOf(oldNode);

        rootNode.Nodes.Remove(oldNode);
        rootNode.Nodes.Insert(
        index,
        newNode);
    }

    Quess who? :D
    • Proposed as answer by Hetraq Saturday, September 13, 2008 3:23 PM
    Saturday, September 13, 2008 3:23 PM