开发者

Copy all treeView parent and children to another treeView c# WinForms

开发者 https://www.devze.com 2023-02-24 06:43 出处:网络
I am trying to copy the entire tree (exactly all nodes) of a treeview (completely) to another treeview using this code:

I am trying to copy the entire tree (exactly all nodes) of a treeview (completely) to another treeview using this code:

        TreeNodeCollection myTreeNodeCollection = treeView1.Nodes;

        TreeNode[] myTreeNodeArray = new TreeNode[treeView1.Nodes.Count];

        treeView1.Nodes.CopyTo(myTreeNodeArray, 0);

        treeView2.Nodes.AddRange(myTreeNodeArray);

But this does not allow me to do so, it asks to either delete th开发者_JS百科e nodes in source treeview or use it Clone! How can I do that? I dont want my source treeview to lose anything in this process.

** UPDATE ** Ok guys I found a complicated code (for me!!) but how can I use this?

    public static T DeepTreeCopy<T>(T obj)
    {
        object result = null;
        using (var ms = new MemoryStream())
        {
            var formatter = new BinaryFormatter();
            formatter.Serialize(ms, obj);
            ms.Position = 0;
            result = (T)formatter.Deserialize(ms); ms.Close();
        }
        return (T)result;
    } 


try this

public void Copy(TreeView treeview1, TreeView treeview2)
{
    TreeNode newTn;
    foreach (TreeNode tn in treeview1.Nodes)
    {
        newTn = new TreeNode(tn.Text, tn.Value);
        CopyChilds(newTn, tn);
        treeview2.Nodes.Add(newTn);
    }
}

public void CopyChilds(TreeNode parent, TreeNode willCopied)
{
    TreeNode newTn;
    foreach (TreeNode tn in willCopied.ChildNodes)
    {
        newTn = new TreeNode(tn.Text, tn.Value);
        parent.ChildNodes.Add(newTn);
    }
} 

My regards


Like MohD's answer, but with recursion to get all nodes. (Nodes of childnodes)

    public void CopyTreeNodes(TreeView treeview1, TreeView treeview2)
    {
        TreeNode newTn;
        foreach (TreeNode tn in treeview1.Nodes)
        {
            newTn = new TreeNode(tn.Text, tn.ImageIndex, tn.SelectedImageIndex);
            CopyChildren(newTn, tn);
            treeview2.Nodes.Add(newTn);
        }
    }
    public void CopyChildren(TreeNode parent, TreeNode original)
    {
        TreeNode newTn;
        foreach (TreeNode tn in original.Nodes)
        {
            newTn = new TreeNode(tn.Text, tn.ImageIndex, tn.SelectedImageIndex);
            parent.Nodes.Add(newTn);
            CopyChildren(newTn, tn);
        }
    } 


Using the node.Clone() function works as well...

foreach (TreeNode node in treeViewSource.Nodes)
{
    treeViewTarget.Nodes.Add((TreeNode)node.Clone());
}

Adding a root node will help ensure the "PlusMinus" functionality is viewable.

TreeNode rootNode = new TreeNode("Root Node");
treeViewTarget.Nodes.Add(rootNode);
foreach (TreeNode node in treeViewSource.Nodes)
{
    rootNode.Nodes.Add((TreeNode)node.Clone());
}
rootNode.Expand();


Thank you guys for the Answers. Combining all answers this worked for me. Even if the treeview has multiple levels.

  public void CopyTreeNodes(TreeView treeview1, TreeView treeview2)
    {
        TreeNode newTn;
        foreach (TreeNode tn in treeview1.Nodes)
        {
            newTn = new TreeNode(tn.Text, tn.Value);
            CopyChildren(newTn, tn);
            treeview2.Nodes.Add(newTn);
        }
    }

 public void CopyChildren(TreeNode parent, TreeNode original)
    {
        TreeNode newTn;
        foreach (TreeNode tn in original.ChildNodes)
        {
            newTn = new TreeNode(tn.Text, tn.Value);
            parent.ChildNodes.Add(newTn);
            CopyChildren(newTn, tn);
        }
    }


You can use this at the desired level

     public void RootCopy(TreeView treeview1, TreeView treeview2)
    {
        TreeNode newNode;
        foreach (TreeNode tnode in treeview1.Nodes)
        {
            newNode = new TreeNode(tnode.Text);
            treeview2.Nodes.Add(newNode);
            if (tnode.Nodes.Count != 0)
            {
                int _1index = tnode.Index;
                ChildsCopyLevel2(_1index, treeview1, treeview2);
            }


        }

    }


    public void ChildsCopyLevel2(int index1, TreeView TV1, TreeView TV2)
    {
        foreach (TreeNode Tnode in TV1.Nodes[index1].Nodes)
        {
            string Childtext = Tnode.Text;
            TV2.Nodes[index1].Nodes.Add(Childtext);
            if (Tnode.Nodes.Count != 0)
            {// ChildsCopyLevel3(Tnode.Nodes.Count, TV1, TV2);
                int _2index = Tnode.Index;
                ChildsCopyLevel3(index1, _2index, TV1, TV2);
            }
        }

    }


    public void ChildsCopyLevel3(int index1, int index2, TreeView TV1, TreeView TV2)
    {
        foreach (TreeNode Tnode in TV1.Nodes[index1].Nodes[index2].Nodes)
        {
            string Childtext = Tnode.Text;
            TV2.Nodes[index1].Nodes[index2].Nodes.Add(Childtext);
        }
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消