Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Copy treeNodeCollection problem

Copy treeNodeCollection problem

Scheduled Pinned Locked Moved C#
helpquestion
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sepel
    wrote on last edited by
    #1

    hi. how can copy all nodes of a treeview in another at once? i dont want to use foreach methode.

    sepel

    E D 2 Replies Last reply
    0
    • S sepel

      hi. how can copy all nodes of a treeview in another at once? i dont want to use foreach methode.

      sepel

      E Offline
      E Offline
      Eduard Keilholz
      wrote on last edited by
      #2

      foreach (Node ndSource in treeSource.Nodes)
      {
      Node newNode = new Node();
      newNode.Text = ndSource.Text;
      -- copy all required properties like the text property above --
      ChildNodes(ndSource, newNode);
      treeDestination.Nodes.Add(newNode);
      }

      private void ChildNodes(Node ParentNode, Node newParent)
      {
      foreach (Node ndChild in ParentNode.Nodes)
      {
      Node newNode = new Node();
      newNode.Text = ndChild.Text;
      -- copy all required properties like the text property above --
      ChildNodes(ndChild);
      newParent.Nodes.Add(newNode);
      }
      }

      .: I love it when a plan comes together :. http://www.zonderpunt.nl

      1 Reply Last reply
      0
      • S sepel

        hi. how can copy all nodes of a treeview in another at once? i dont want to use foreach methode.

        sepel

        D Offline
        D Offline
        DaveyM69
        wrote on last edited by
        #3

        A node can't exist in two treevies at once. Each node has to be cloned first so some sort of loop is inevitable. Two code snippets below. The first does what you want but with a loop. The second moves the nodes rather than copies but no loop required.

        // Copies the nodes
        if (treeView1.Nodes.Count > 0)
        {
        TreeNodeCollection treeNodes = treeView1.Nodes;
        TreeNode[] nodearray = new TreeNode[treeNodes.Count];
        for (int i = 0; i < nodearray.Length; i++)
        {
        nodearray[i] = (TreeNode)treeNodes[i].Clone();
        }
        treeView2.Nodes.AddRange(nodearray);
        }

        //Moves the nodes
        if (treeView1.Nodes.Count > 0)
        {
        TreeNodeCollection treeNodes = treeView1.Nodes;
        TreeNode[] nodearray = new TreeNode[treeNodes.Count];
        treeNodes.CopyTo(nodearray, 0);
        //Clear the original tree as nodes can't exist in both
        treeView1.Nodes.Clear();
        treeView2.Nodes.AddRange(nodearray);
        }

        Dave
        BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
        Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)

        S 1 Reply Last reply
        0
        • D DaveyM69

          A node can't exist in two treevies at once. Each node has to be cloned first so some sort of loop is inevitable. Two code snippets below. The first does what you want but with a loop. The second moves the nodes rather than copies but no loop required.

          // Copies the nodes
          if (treeView1.Nodes.Count > 0)
          {
          TreeNodeCollection treeNodes = treeView1.Nodes;
          TreeNode[] nodearray = new TreeNode[treeNodes.Count];
          for (int i = 0; i < nodearray.Length; i++)
          {
          nodearray[i] = (TreeNode)treeNodes[i].Clone();
          }
          treeView2.Nodes.AddRange(nodearray);
          }

          //Moves the nodes
          if (treeView1.Nodes.Count > 0)
          {
          TreeNodeCollection treeNodes = treeView1.Nodes;
          TreeNode[] nodearray = new TreeNode[treeNodes.Count];
          treeNodes.CopyTo(nodearray, 0);
          //Clear the original tree as nodes can't exist in both
          treeView1.Nodes.Clear();
          treeView2.Nodes.AddRange(nodearray);
          }

          Dave
          BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
          Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)

          S Offline
          S Offline
          sepel
          wrote on last edited by
          #4

          but i think to use AddRang function.

          sepel

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups