I try to paste some copied objects using Clipboard class.
<Serializable()> Public Class DogsZoo
Public Property Dogs As List(Of Dog)
Public Property Workers As List(Of Worker)
Public Sub New(dogs As List(Of Dog), workers As List(Of Worker))
Me.Dogs = dogs
Me.Workers = workers
End Sub
End Class
Dim myDogsZoo = myCity.GetDogsZoo()
Clipboard.SetData("dogs", myDogsZoo)
' bla bla , some actions '
If Not Clipboard.ContainsData("dogs") Then Throw New Exception("Clipboard")
' here I obtain Nothing !?'
Dim clipboardObject = Clipboard.GetData("dogs")
The verification Clipboard.ContainsData(myFormat) passes successfully, but when I try to obtain the data I obtains null (Nothing). Is it a correct behavior?
PS.
C# or VB.NET answers are both OK.
PPS.
I should recognize, I used the method above without any problems with Clipboard, when the datatypes where simple (a generic List). Now, I changed the object to keep in the memory to a custom one... and from that time... this problem...
If your using a custom object, I believe the object must be support serialization.
I found the following article helpful when I was investigating reading images and text from the clipboard: http://msdn.microsoft.com/en-us/library/637ys738.aspx
There is a section about writing data to the clipboard in a custom format. I notice you code in VB.net so I've copied the VB.net extract as follows:
Hope this helps?
Kind Regards, Wayne
Be careful that both "Dog" and "Worker" class be serializable...
The object to be copied needs to be Serializable, that means, at least all its public members needs to be serializable.
If not, the clipboard operation fails silently.
Also, the thread that accedes the Clipboard should have the STA attribute...
I know this is a really old post but as it does not have a solution I thought I would provide one (my initial research lead me here but I stumbled across the cause of the problem elsewhere).
To stop my object having the same problem described in this thread I had to ensure that it contained a constructor with two parameters, as shown below. Once implemented the pasting worked perfectly.
Obviously the code within the With - End With block will be specific to the properties of your own custom object.