We help IT Professionals succeed at work.

How do I programmatically using "copy" context menu item to copy a TreeNode from a RadTreeView to a Clipboard

sabrina_spillane
on
In a Window's Application, I have a RadTreeView and on right click of the TreeView a ContextMenu appears which contains items called Copy and Paste. On right click of a selected node and choosing Copy item from ContextMenu how do I copy the current selected node to a Clipboard and then Paste in another location.

I am just unsure what format I should save it as to the clipboard so that when I retrieve it again it will be paste as a TreeNode again. I check the Formats for Clipboard and none of them are suited for a TreeNode?

Any help would be greatly appreciated.
Comment
Watch Question

I achieved what you want by first serializing the object into a string and simply saving that string to the clipboiard.  I can then deserialize and cast

Andrew
       private void button1_Click(object sender, EventArgs e)
        {
            TreeNode t = new TreeNode("ClipBoardNode");
            Clipboard.SetData("TreeNode", Serialize(t));
        }
 
        private void button2_Click(object sender, EventArgs e)
        {
            TreeNode retrieved = (TreeNode)DeSerialize(Clipboard.GetData("TreeNode").ToString());
            treeView1.Nodes.Add(retrieved);
        }
 
        private string Serialize(object objectToSerialize)
        {
            string serialString = null;
            using (System.IO.MemoryStream ms1 = new System.IO.MemoryStream())
            {
                BinaryFormatter b = new BinaryFormatter();
                b.Serialize(ms1, objectToSerialize);
                byte[] arrayByte = ms1.ToArray();
                serialString = Convert.ToBase64String(arrayByte);
            }
            return serialString;
        }
        private object DeSerialize(string serializationString)
        {
            object deserialObject = null;
            byte[] arrayByte = Convert.FromBase64String(serializationString);
            using (System.IO.MemoryStream ms1 = new System.IO.MemoryStream(arrayByte))
            {
                BinaryFormatter b = new BinaryFormatter();
                deserialObject = b.Deserialize(ms1);
            }
            return deserialObject;
        }

Open in new window

Author

Commented:
Hi there,
Thanks for all the help so far. I did make the mistake in saying it was a Treenode however I am using the Telerik Controls so it's a RadTreeNode that I am using instead. Because of this the Serialize() is throwing an exception. I tired to research it in Telerik. RadTreeNode butit isn't giving me anything back as of yet. Here is the Serialize Exception Error:

Serialization Exception was unhandled:
Type 'Telerik.WinControls.UI.RadTreeNode' in Assembly 'Telerik.WinControls.UI, Version=5.0.0.0, Culture=neutral, PublicKeyToken=5bb2a467cbec794e' is not marked as serializable.

Any idea's? thanks again for helping me out on this one so far.

Author

Commented:
i meant to also say this is the line where I am recieving the Serialzation Exception Error:

 b.Serialize(ms1, objectToSerialize);
my solution will not work unfortunately as that control you are using is not serializable.  I did not realise.

Author

Commented:
Thats what i am seeing on there website too, it's extremly annoying. Is there a work around do you think?
Any help would be greatly appriciated.

Author

Commented:
I wanted to know about RadTreeNode Control not just TreeNode so this solution wasn't the answer to my problem but it does anwser the TreeNode if anyone is interested.