?
Solved

Using Clipboard for Saving Objects

Posted on 2009-04-20
10
Medium Priority
?
293 Views
Last Modified: 2012-05-06
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

0
Comment
Question by:hlominac
  • 6
  • 4
10 Comments
 
LVL 39

Expert Comment

by:abel
ID: 24183837
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
 
LVL 39

Expert Comment

by:abel
ID: 24183845
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
 

Author Comment

by:hlominac
ID: 24184087
Errors:
MyUserClass is not defined
SetData is not a member ... I used SetDataObject
What does MyUserClass contain?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 39

Expert Comment

by:abel
ID: 24185078
> 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
 

Author Comment

by:hlominac
ID: 24185338
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
 
LVL 39

Expert Comment

by:abel
ID: 24185404
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
 
LVL 39

Accepted Solution

by:
abel earned 2000 total points
ID: 24185706
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
 

Author Comment

by:hlominac
ID: 24186409
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
 

Author Closing Comment

by:hlominac
ID: 31572184
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
 
LVL 39

Expert Comment

by:abel
ID: 24186576
> 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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question