Clipboard in VB Net

Dunamis

[H]Lurker Supreme[/H]
Joined
Jun 30, 2004
Messages
2,301
I'm want to store an object in the clipboard and retrieve it later on. This object is an instance of my own class that aren't pre-defined DataFormats.

I have this code to store MyObject into clipboard
Code:
Clipboard.SetDataObject(MyObject, True)

And this code try to retrieve the object back from clipboard
Code:
Dim MyObject As MyClass = Clipboard.GetDataObject().GetData(MyClass.GetType)
If MyObject Is Nothing Then Exit Sub

The problem is that MyObject returned from the clipboard is always null (Nothing in VB)

Whats wrong here?
 
No, but I've tried adding the <Serializable()> attribute to MyClass, didn't do nothing.
Does it need some kind of special method to pull it out of clipboard to deserialize it or something?
 
Clipboard.GetFormats returns a string array, what are the contents of the array when you call it?
 
If you want to use the clipboard on a whole new object type you need to make your class implement the clipboard object interface ("Implements IDataObject").

The reference to the IDataObject interface is in Windows.System.Forms.dll if you're making a non-forms app, otherwise it's already included.
 
I don't think that's true.
E.x.: The string class doesn't implement IDataObject, yet it can be stored in the clipboard =]

MSDN said:
Because the data type of the object returned from the clipboard can vary, this method returns the data in an IDataObject. Then you can use methods of the IDataObject interface to extract the data in its proper data type.
 
HJB417 said:
I don't think that's true.
E.x.: The string class doesn't implement IDataObject, yet it can be stored in the clipboard =]
That's because text is a standard clipboard data type. An arbitrary object is not.

Your MSDN quote says exactly what I said: he needs to implement the IDataObject interface in his class. :p
 
HJB417 said:
Clipboard.GetFormats returns a string array, what are the contents of the array when you call it?
Yeh, I've done that too, it return the name of my class so it would output something like this "MyNamespace.MyClass"
 
pxc said:
That's because text is a standard clipboard data type. An arbitrary object is not.

Your MSDN quote says exactly what I said: he needs to implement the IDataObject interface in his class. :p
I think this is worth a try, thanks guys :)
 
Back
Top