Winform: How to copy treenode collection in clipboard with Tag property

Sudip Bhatt 2,276 Reputation points
2020-11-16T16:15:47.483+00:00

Suppose i have a treeview and a particular node has few sub node and sub node has another nodes etc or particular node has no sub nodes. in my case i have assign a object to tag property of each node.

Now i want when user select any node and click button then i want to store that node & its all sub nodes in clipboard. i tried a code example which is working but Tag property getting null.

So please advise how could i store nodes in clipboard with tag property ?

i test this code.

    private void pasteToolStripMenuItem_Click(object sender, EventArgs e)
            {
                if (treeView1.SelectedNode != null)
                {
                    TreeNode paste = treeView1.SelectedNode;
                    IDataObject data = Clipboard.GetDataObject();
                    if (data.GetDataPresent(typeof(TreeNode)))
                    {
                        TreeNode element = (TreeNode)data.GetData(typeof(TreeNode));
                        TreeNode tn = new TreeNode();
                        tn.Text = element.Text;
                        tn.Name = element.Name;
                        foreach (TreeNode temp in element.Nodes)
                        {
                            TreeNode newTn = new TreeNode(temp.Text);
                            tn.Nodes.Add(newTn);
                        }
                        if (paste.Parent != null)
                        {
                            paste.Parent.Nodes.Insert(paste.Index + 1, tn);

                        }
                        else
                        {
                            this.treeView1.Nodes.Insert(paste.Index + 1, tn);
                        }
                    }
                }
            }



            private void cutToolStripMenuItem_Click(object sender, EventArgs e)
            {
                TreeNode node = this.treeView1.SelectedNode;
                if (node != null)
                {
                    Clipboard.SetDataObject(node, true);
                    this.treeView1.Nodes.Remove(node);
                }

            }

please tell me a easy to store nodes and its tag property in clipboard. thanks

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,884 questions
0 comments No comments
{count} votes

Accepted answer
  1. Tianyu Sun-MSFT 30,351 Reputation points Microsoft Vendor
    2020-11-17T08:02:18.307+00:00

    Hi @Sudip Bhatt ,

    Thank you for posting this issue in Microsoft Q&A forum.

    According to your code snippets, please try to add these two code lines:

    tn.Tag = element.Tag;

    newTn.Tag = temp.Tag;

    And the codes should be looked like this:

    if (data.GetDataPresent(typeof(TreeNode)))  
    {  
         TreeNode element = (TreeNode)data.GetData(typeof(TreeNode));  
         TreeNode tn = new TreeNode();  
         tn.Text = element.Text;  
         tn.Name = element.Name;  
         tn.Tag = element.Tag;  
         foreach (TreeNode temp in element.Nodes)  
         {  
               TreeNode newTn = new TreeNode(temp.Text);  
               newTn.Tag = temp.Tag;  
               tn.Nodes.Add(newTn);  
         }  
    
         //……  
    
    }  
    

    The result:

    40342-test1.gif

    Sincerely,
    Tianyu

    • If the answer is helpful, please click "Accept Answer" and upvote it.
      Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.