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

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.
sabrina_spillaneAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

REA_ANDREWCommented:
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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sabrina_spillaneAuthor 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.
0
sabrina_spillaneAuthor Commented:
i meant to also say this is the line where I am recieving the Serialzation Exception Error:

 b.Serialize(ms1, objectToSerialize);
0
Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

REA_ANDREWCommented:
my solution will not work unfortunately as that control you are using is not serializable.  I did not realise.
0
sabrina_spillaneAuthor 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.
0
sabrina_spillaneAuthor 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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.