Click here to Skip to main content
12,633,472 members (30,239 online)
Click here to Skip to main content
Add your own
alternative version


52 bookmarked

Saving and obtaining custom objects to/from Windows Clipboard

, 26 Aug 2004
Rate this:
Please Sign up or sign in to vote.
The code shows how to save (and restore) an instance of your own class to clipboard.


This article shows, how to save an instance of a custom C#.NET class (any class) to a clipboard. It explains what requirements your class has to meet, so that you can save it to the clipboard.


Surprisingly, but I've found a very few materials on this topic on the Internet. There are thousands of posts like "help, when I retrieve data from clipboard, it always returns null". Most of the answers I've found make contrary statements, some of them say that "you can't save a reference-type object to a clipboard", and other crap like this.

So, colleagues, let me save your time and provide you with some info.


Basically, serialization is a process of "saving" an object to a form, that can be transported, copied to hard disk, sent by email etc. And vice versa, deserialization is making an instance of an object from this form (for example, from a byte stream). And this is the first and the only clipboard requirement for your class. Your class has to be serializable.

To make a class serializable, you can implement a ISerializable interface, but the easiest way to make a class serializable is to mark it with the Serializable attribute. Like this:

public class Document
  public int documentID = 0;
  public string documentDescription = "";
// that was simple huh?

Important: please note, that if your class is derived from some base class, the base class also has to be marked as Serializable. Like this:

public class Document
  public int documentID = 0;
  public string documentDescription = "";
public class EmailDocument : Document {
  public string subject = "";
  public string body = "";

Also note, that if your class has some complex private properties, that can't be serialized (or you don't want them to be serialized), you should mark such properties with [NonSerialized()] attribute like this:

public class Document
  public int documentID = 0;
  public string documentDescription = "";

  private System.Data.SqlClient.SqlCommand cmd;
  private System.Data.SqlClient.SqlConnection cn;

Determine if your object is serializable or not

To determine if your class is clipboard compatible, you can write a simple method IsSerializable(object obj) (see listing below). It uses a BinaryFormatter class to serialize your object to a MemoryStream. This method requires some memory, so use it only for debugging purposes, remove it from your app when finished testing. Here it is:

using System.Runtime.Serialization.Formatters.Binary;
private static bool IsSerializable(object obj)
  System.IO.MemoryStream mem = new System.IO.MemoryStream();
  BinaryFormatter bin = new BinaryFormatter();
    bin.Serialize(mem, obj);
    return true;
  catch(Exception ex)
    MessageBox.Show("Your object cannot be serialized." + 
                     " The reason is: " + ex.ToString());
    return false;

Clipboard Formats

Other things we have to learn is data formats. Every application stores some data to the clipboard in some data format (or in multiple data formats). There are standard formats like Text, Image, Wave Audio, HTML text etc. These formats are pre-registered on every Windows-computer. Any application "knows", what formats it "understands", so an application "knows" if the data at the clipboard can be pasted to it. An application can store some data using one of these standard formats, or register its own data format.

.NET framework Clipboard class

Accessing the Clipboard and storing data there with a Windows application is made possible through the use of the SetDataObject() method of a Clipboard class, which stores the data on the Clipboard using the IDataObject interface. Retrieving data from the Clipboard is also done through the IDataObject interface.

Copy to clipboard

To save your custom object to a clipboard, you need to register your custom clipboard format (if it's not already registered), create an instance of a DataObject filled with your data, and pass this instance to a ClipBoard.SetDataObject() method. To register your own custom data format, all you have to do is to think up a name for your data format! And then call a static member DataFormats.GetFormat(). Personally, I think that the best name for a new data format is a full name of its type.

Just look at the code. I've added CopyToClipboard() method to my above Document class:

public class Document
  public int documentID = 0;
  public string documentDescription = "";

  private System.Data.SqlClient.SqlCommand cmd;
  private System.Data.SqlClient.SqlConnection cn;
  public void CopyToClipboard()
    //register my custom data format with Windows
    //or get it if it's already registered
    DataFormats.Format format = 

    //now copy to clipboard
    IDataObject dataObj = new DataObject();
    dataObj.SetData(format.Name, false, this);
    Clipboard.SetDataObject(dataObj, false);
    //that's it

Obtain data from clipboard

First of all, before getting some data from clipboard, you have to ensure that the data format in the clipboard is compatible with your app. To do this, call GetDataPresent() method of a DataObject instance. After that, you call a GetData() method of a IDataObject interface. Here's the code, where I get my Document object from clipboard:

protected static Document GetFromClipboard()
    Document doc = null;
    IDataObject dataObj = Clipboard.GetDataObject();
    string format = typeof(Document).FullName;
        doc = dataObj.GetData(format) as Document;
    return doc;


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Alex Yumas
Founder Jitbit
United Kingdom United Kingdom
I'm CEO/Founder and lead developer at Jitbit Software

You may also be interested in...


Comments and Discussions

QuestionLatest version of Source code? Pin
Joginder17-May-13 12:11
memberJoginder17-May-13 12:11 
GeneralMy vote of 5 Pin
Stefano Manni18-Apr-13 1:58
memberStefano Manni18-Apr-13 1:58 
GeneralMy vote of 5 Pin
akerd13-Feb-13 22:48
memberakerd13-Feb-13 22:48 
GeneralMy vote of 5 Pin
jpsstavares3-Oct-11 5:48
memberjpsstavares3-Oct-11 5:48 
GeneralMy vote of 5 Pin
Alex Essilfie11-Aug-11 7:06
memberAlex Essilfie11-Aug-11 7:06 
Questiondon't share demo program? Pin
r1co17-Jan-09 20:31
memberr1co17-Jan-09 20:31 
AnswerRe: don't share demo program? Pin
Alex Essilfie11-Aug-11 6:52
memberAlex Essilfie11-Aug-11 6:52 
GeneralThank You Pin
ixup4-Aug-08 23:29
memberixup4-Aug-08 23:29 
GeneralRe: Thank You Pin
Alex Essilfie11-Aug-11 7:03
memberAlex Essilfie11-Aug-11 7:03 
GeneralIs it possible to serialize class object in smartphone application Pin
guocang25-Jul-06 8:38
memberguocang25-Jul-06 8:38 
NewsNot really copying object instance Pin
Le_MuLoT8-Nov-05 4:15
memberLe_MuLoT8-Nov-05 4:15 
thanks for this great article ! But I remark that, in my case, if I copy an object instance, modifying it, and paste it... the new object instance got the new modifications instead of not ! And I noticed, by placing some break points into my surcharged constructor and the GetObjectData() function, that the Copy/Paste process don't seems to make a "real" binary serialization at all (the break points didn't stop anything).

So, I found a pretty easy solution that, I hope, will be usefull. My way, is to "force" a serialization into a MemoryStream and put the stream content to clipboard.

DataFormats.Format format = 			DataFormats.GetFormat(typeof(MyClass).FullName);
			IDataObject dataObj = new DataObject();
				System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bformatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
// to here, nothing changed

// now -forcing serialization- into a memorystream 
System.IO.MemoryStream stream = new System.IO.MemoryStream();

bformatter.Serialize(stream, MyClasseObjectInstance);


// Now copying the array of bytes to clipboard
dataObj.SetData(typeof(MyClass).FullName, stream.ToArray());
				Clipboard.SetDataObject(dataObj, true);

//... Pasting
IDataObject dataObj = Clipboard.GetDataObject();
			string format = typeof(MyClass).FullName;

byte[] memory = dataObj.GetData(format) as byte[];
System.IO.MemoryStream stream = new System.IO.MemoryStream(memory);
			System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bformatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
MyNewObjectInstance = (MyClass)bformatter.Deserialize(stream);

//That's it

I tried to directly put the MemoryStream to the ClipBoard, but I guess that isn't a serializable class... whatever, each time I got a null reference... so, dealing directly with dynamical array of byte, the problem is solved.
GeneralMy object is serializable but still the GetData method returns null. Pin
Zebster13-Jun-05 3:09
memberZebster13-Jun-05 3:09 
GeneralRe: My object is serializable but still the GetData method returns null. Pin
Le_MuLoT7-Nov-05 8:33
memberLe_MuLoT7-Nov-05 8:33 
GeneralRe: My object is serializable but still the GetData method returns null. Pin
sizzlinmo7-Aug-08 6:26
membersizzlinmo7-Aug-08 6:26 
GeneralRe: My object is serializable but still the GetData method returns null. Pin
stebo07289-Oct-12 4:42
memberstebo07289-Oct-12 4:42 
GeneralRe: My object is serializable but still the GetData method returns null. Pin
stebo07289-Oct-12 5:58
memberstebo07289-Oct-12 5:58 
GeneralAlso with non-binary serialization Pin
JesusAbizanda8-Jun-05 3:01
memberJesusAbizanda8-Jun-05 3:01 
GeneralThank you - great work! Pin
infoli30-Dec-04 4:06
sussinfoli30-Dec-04 4:06 
GeneralDetermining the clipboard object's attributes.. Pin
Andyzyx7-Sep-04 12:47
memberAndyzyx7-Sep-04 12:47 
GeneralRe: Determining the clipboard object's attributes.. Pin
AanandarajanP9-Nov-05 3:29
memberAanandarajanP9-Nov-05 3:29 
GeneralFields vs. properties Pin
ip25531-Aug-04 11:40
memberip25531-Aug-04 11:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.161208.2 | Last Updated 27 Aug 2004
Article Copyright 2004 by Alex Yumas
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid