views:

46

answers:

2

I have a c# project using version 4.0 of the .net framework, and running on VS 2010. I have created a tree view populated with some extended tree nodes. I want the user to be able to copy, cut, and paste these nodes to the clipboard via a context menu or keyboard shortcuts (and not just drag-drop).

The code runs fine when copying, but when I try to paste these nodes it throws this error: Unable to cast object of type 'System.IO.MemoryStream' to type 'Namespace Path.TreeNodeEx'.

Here's my cut/copy/paste methods.

public void Copy()
{
    Clipboard.SetData("Tree Node Ex", CurrentTreeNode.Clone());
}


public void Paste()
{
    CurrentTreeNode.Nodes.Add((TreeNodeEx)Clipboard.GetData("Tree Node Ex"));
}

I suspect the problem is something to do with serialization, but I've tried implement the ISeralizable Interface and the [Serializable] attribute to no avail.

Any suggestions?

A: 

Try this:

Clipboard.GetDataObject().GetData(typeof(TreeNodeEx))
leppie
I tried this but it still returned null. I also change the copy method to Clipboard.SetDataObject(), but that still gave a null node.
fneep
A: 

It turns out that attached to each extended tree node I had a dictionary that stored extra information. Apparently you can't serialize dictionaries, so this was preventing any of the tree nodes from being serialized.

I implemented ISerializable for these extended tree nodes, and then converted the dictionary into two lists, which I then converted back to a dictionary in the deserialize constructor.

fneep