Using Clipboard for Saving Objects

I wish to save a class object on the clipboard and later retrieve it.  I probably should use some form of cast to make it work.  How do I do this?
Thanks, Harold
Public Class ObjData
      Public Image As Image
      Public Text As String
      Public Path As String
   End Class
 
Public MyObject As New ObjData
 
'Save to clipboard
Clipboard.SetDataObject(MyObject, True)
 
'Get from clipboard
MyObject = Clipboard.GetDataObject()

Open in new window

hlominacAsked:
Who is Participating?
 
abelConnect With a Mentor Commented:
Using only 1.1 methods below, should help you further. As it appears the 2.0 methods were only convenience methods, now you just need to do a bit more by hand. Here you go (the rest stays the same: the store on the Serializable attribute):

' set it '
Dim usr As ObjData = New ObjData()
Dim dataObj As DataObject = New DataObject()
dataObj.SetData(GetType(ObjData), usr)
Clipboard.SetDataObject(dataObj, False)
 
' retrieve it '
Dim cbData As DataObject = Clipboard.GetDataObject()
If cbData.GetDataPresent(ObjData.Format.Name) Then
    Dim usrRetrieved As ObjData = CType(cbData.GetData(GetType(ObjData)), ObjData)
End If

Open in new window

0
 
abelCommented:
It is a tad tricky, but not that much. Add the <Serializable()> attribute to your class and you can this:

Dim obj As MyUserClass = New ObjData()
Clipboard.SetData(DataFormats.Serializable, obj)
Dim cbData As DataObject = Clipboard.GetDataObject()
If cbData.GetDataPresent(DataFormats.Serializable) Then
    Dim objRetrieved As ObjData= CType(cbData.GetData(DataFormats.Serializable, False), ObjData)
End If

Open in new window

0
 
abelCommented:
Just to make it complete, with adding the Serializable attribute I mean this:

<Serializable()> _
Public Class ObjData
      Public Image As Image
      Public Text As String
      Public Path As String
End Class

Open in new window

0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
hlominacAuthor Commented:
Errors:
MyUserClass is not defined
SetData is not a member ... I used SetDataObject
What does MyUserClass contain?
0
 
abelCommented:
> What does MyUserClass contain?

that's not important, that was an example class. Replace it with your ObjData class.

SetData is a member. Do not use SetDataObject. What version of .NET are you using? I assumed 2.0 or higher.
0
 
hlominacAuthor Commented:
I'm using Microsoft Visual Basic .NET  
Development Environment 2003 Version 7.1.3008
.NET Framework 1.1 Version 1.1.4322SP1
Clipboard Members are: SetDataObject and GetDataObject
0
 
abelCommented:
aha, that changes the topic significantly. It's probably a good idea that you give that information in your question next time, because experts listening in here assume .NET 2.0 or 3.5 at the very least, because it is already so old and support is discontinued from MS. There's also a zone for 1.1 specific question here at EE.

I'll try to dig up a 1.1 version, but you'll have to help here because I don't have 1.1 installed.
0
 
hlominacAuthor Commented:
I got it to work using your code examples.  Thank you so very much fot the extra work.
The code snippit shows before and after using my nomenclature.  What I needed was the DataObject.
'Save to clipboard (before)
Clipboard.SetDataObject(MyObject, True)
 
'Changed to:
Dim dataObj As DataObject = New DataObject
dataObj.SetData(GetType(ObjData), MyObject)
Clipboard.SetDataObject(dataObj, True)
 
 
'Get from clipboard (before)
MyObject = Clipboard.GetDataObject()
 
'Changed to:
Dim MyData As DataObject = Clipboard.GetDataObject()
SaveObject = CType(MyData.GetData(GetType(ObjData)

Open in new window

0
 
hlominacAuthor Commented:
I had to experiment with the code, and of course I needed to save the data on the clipboard.  I've been retired nearly 19 years and write code for non-profit organizations, so resources to ugrade to .NET 2.0 does not fit my budget
0
 
abelCommented:
> I've been retired nearly 19 years and write code for non-profit organizations, so resources to ugrade to .NET 2.0 does not fit my budget

np, I understand. It will be helpful in getting the right answers more quickly if you select the 1.1 zone next to this zone.

> I got it to work using your code examples.  Thank you so very much fot the extra work.
glad you got it working. It's a bit of a tricky interface and not really intuitive.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.