Click here to Skip to main content
15,176,980 members
Articles / Programming Languages / C#
Posted 3 Jun 2002


54 bookmarked

Clipboard handling with .NET - Part II

Rate me:
Please Sign up or sign in to vote.
4.71/5 (12 votes)
3 Jun 20025 min read
Builds on part 1, showing how to use custom/multiple formats

Screenshot of the demo application

Formats that are available after clicking Copy All


At the end of April 2002, Nish and I were trying to figure out some of .NET's clipboard capabilities. When we had finished the conversation we decided on writing a two part article, Nish doing part one on the basics then I would do part two on some more advanced topics. I was going to write it as soon as the screensaver competition was over, needless to say I forgot....until now that is.

In this article I hope to cover multiple data formats, and creating your own custom data formats.

Multiple data formats

Placing multiple data formats on the clipboard is incredibly easy; once you know the trick :-)

The first step is to figure out what data formats you will be using. Later I'll discuss custom formats so ignore those for now. To help you in your search the DataFormats class has a list of commonly used formats stored as static string fields; refer to MSDN for the specifics on each format listed there.

Now that you know the formats you will use, create a instance of a class that implements IDataObject, the only such class is (appropriately) named DataObject.

IDataObject ido = new DataObject();

With the IDataObject interface in hand we can begin to use it. The purpose of the IDataObject is to "[Provide] a format-independent mechanism for transferring data" (MSDN, IDataObject interface). Those familiar with COM will know this object well. It can store one object of each format that you pass in. Thus if you need to store 3 Text formatted objects, you'll need to wrap them into a custom format which I'll explain later.

ido.SetData(DataFormats.Text, true, myString);

That line of code adds a new Text formatted object with the value of myString to the DataObject, and allows it to be converted to other types as well. This will be a key point that you find in Win32 using the Clipboard. Data is often in multiple formats to allow it to be used in a multitude of programs. Typically data will be stored in a custom format, as text, and possibly a bitmap if it represents something graphical. This allows for pasting into many applications and into itself in a native format.

Adding multiple, additional, formats is as easy as additional calls to SetData with differing formats.

Clipboard.SetDataObject(ido, true);

This line finally places the data on the clipboard where it can be read by any program. The true argument tells the Clipboard to make the data available even after the program quits. Later I'll introduce another important point about the second parameter, which isn't mentioned by MSDN.

With the data copied to the Clipboard you can now paste it as you did before; check for the proper data format then retrieve it if it exists.

IDataObject ido = Clipboard.GetDataObject();

    // Text data is present on the clipboard
    textBox1.Text = (string) ido.GetData(DataFormats.Text);

Custom data formats

A custom data format can be anything; whether a special text formatting such as HTML or XML or it can be an instance of a class. To use a custom format, first you register it with Windows. In the demo application you will find that I register the type with Windows in the static constructor for the custom type class, CustomData.

static CustomData
    format = DataFormats.GetFormat(

The code typeof(CustomData).FullName merely retreives the full name of the type; since I needed to choose a unique name I figured that would do. GetFormat returns an instance of the DataFormats.Format class which I store for use as a static property to access the Name and ID of the format. The name doesn't have to be relevant just unique, Windows registers a Format17 which it uses for screenshots.

The only unique part about creating a custom format that uses a class is that the class must be serializable, that is it needs to have the Serializable attribute applied to it.

public class CustomData

To use it on the clipboard you do so as you would any other format.

IDataObject ido = new DataObject();
CustomData cd = new CustomData(myImage, myString);

ido.SetData(CustomData.Format.Name, false, cd);

Retrieving data from it is done the same as before.

IDataObject ido = Clipboard.GetDataObject();

if( ido.GetDataPresent(CustomData.Format.Name) )
    // Do something interesting with the data
    CustomData cd = (CustomData) 

Persisting data on the clipboard, ie the 2nd parameter to SetDataObject

According to MSDN the second parameter tells the Clipboard object whether the data inside should be persisted when the application exits. BUT that isn't the full story. What it actually does is delay the serialization of the data until when the data is actually needed.

To see this in action run the included ClipboardTest2 application, uncheck "Persist Data"; then proceed to Draw, Copy, and Paste, then Draw and Paste. You'll see that even though the data was copied only once the new data is shown.

The reason for this lies in that I continue to operate on the same object that was passed into the IDataObject's SetData method, since we told the Clipboard object NOT to serialize the data until needed, that is what it did. On top of that it will reserialize the data everytime the data is requested. Oddly though it only updates the format specified, none of the auto-converted formats are touched once the initial serialization of the format occurs.

To demonstrate this open up the ClipboardTest2 application, again uncheck "Persist Data"; and then Draw, Copy, and Paste. Proceed to Draw and Paste, then open up your favorite image editing software; MSpaint will do. Now Paste the bitmap there; do some more Draw and Pastes then Paste again in another copy of MSPaint. You'll see the same bitmap was pasted in both MSPaint's.

This seems like a bug, but really it is a bug in my use. Once you place an object on the clipboard the intended result is that object stays the same; not to change it like I did. This means the optimal solution is to create a copy and cache it somewhere until it is needed by the Clipboard object.

If you choose to not persist data to the clipboard, upon exiting the application you should re-place the data on the clipboard and persist it; so that the contents are still there for the user to use.


There you have it folks, the Clipboard in all its glory. Hope I didn't bore you too much. As always leave questions or comments below; and please e-mail me any bugs you find.

For those interested in the Visual Style I used in the screen shots, I got it from; you'll also need StyleXP or the free update to uxtheme.dll from the same site.


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

James T. Johnson
Software Developer (Senior) InfoPlanIT, LLC
United States United States
James has been programming in C/C++ since 1998, and grew fond of databases in 1999. His latest interest has been in C# and .NET where he has been having fun writing code starting when .NET v1.0 was in its first beta.

He is currently a senior developer and consultant for InfoPlanIT, a small international consulting company that focuses on custom solutions and business intelligence applications.

He was previously employed by ComponentOne where he was a Product Manager for the ActiveReports, Data Dynamics Reports, and ActiveAnalysis products.

Code contained in articles where he is the sole author is licensed via the new BSD license.

Comments and Discussions

QuestionThank you! Pin
1242092219-Oct-17 5:17
Member1242092219-Oct-17 5:17 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey17-Apr-12 19:27
professionalManoj Kumar Choubey17-Apr-12 19:27 
GeneralDraging files Pin
Giorgi Dalakishvili17-Nov-08 5:47
mentorGiorgi Dalakishvili17-Nov-08 5:47 
GeneralThis doesn't work for inheritance/interface hierarchies Pin
DavidNaz29-Aug-08 12:01
MemberDavidNaz29-Aug-08 12:01 
Generalclipboard in threads :-( Pin
numpsy b23-Jun-07 12:38
Membernumpsy b23-Jun-07 12:38 
QuestionHow tyo store XmlElement using custom Clipboard format Pin
AmyKar21-Jan-07 21:27
MemberAmyKar21-Jan-07 21:27 
Generalproblem "Clipboard.GetDataObject();" "Object reference not set to an instance of an object" Pin
nip9012-Oct-06 21:55
Membernip9012-Oct-06 21:55 
GeneralRe: problem "Clipboard.GetDataObject();" "Object reference not set to an instance of an object" Pin
NoDozing24-Oct-06 6:30
MemberNoDozing24-Oct-06 6:30 
GeneralRe: problem "Clipboard.GetDataObject();" "Object reference not set to an instance of an object" Pin
fsl_memon24-Jul-08 0:56
Memberfsl_memon24-Jul-08 0:56 
GeneralCustome object makes problem when it calls inside the class. Pin
Anvarksadath24-Aug-06 21:41
MemberAnvarksadath24-Aug-06 21:41 
GeneralClipboard for ArrayList Pin
AdriM27-Jun-06 1:17
MemberAdriM27-Jun-06 1:17 
GeneralJames T. Johnson Pin
alisonstang222-Apr-06 18:23
Memberalisonstang222-Apr-06 18:23 
GeneralRe: James T. Johnson Pin
James T. Johnson22-Apr-06 18:39
MemberJames T. Johnson22-Apr-06 18:39 
GeneralRe: James T. Johnson Pin
alisonstang223-Apr-06 7:06
Memberalisonstang223-Apr-06 7:06 
GeneralRe: James T. Johnson Pin
alisonstang223-Apr-06 7:36
Memberalisonstang223-Apr-06 7:36 
GeneralRe: James T. Johnson Pin
alisonstang223-Apr-06 7:38
Memberalisonstang223-Apr-06 7:38 
GeneralRe: James T. Johnson Pin
alisonstang223-Apr-06 7:40
Memberalisonstang223-Apr-06 7:40 
QuestionRe: James T. Johnson Pin
alisonstang224-Apr-06 16:24
Memberalisonstang224-Apr-06 16:24 
GeneralCustom formats - display on clipboard Pin
Katrien_v10-May-05 1:43
MemberKatrien_v10-May-05 1:43 
Generalcustom formats - DataRow Pin
mike melchi11-Oct-02 9:10
Membermike melchi11-Oct-02 9:10 
GeneralHow I handled this issue Pin
Mike_in_Paradise10-Feb-04 14:06
MemberMike_in_Paradise10-Feb-04 14:06 
GeneralGood one Pin
18-Jun-02 9:33
suss18-Jun-02 9:33 
GeneralRe: Good one Pin
James T. Johnson18-Jun-02 19:30
MemberJames T. Johnson18-Jun-02 19:30 
GeneralCopy metafile to the clipboard Pin
9-Jun-02 21:10
suss9-Jun-02 21:10 
GeneralRe: Copy metafile to the clipboard Pin
James T. Johnson18-Jun-02 19:27
MemberJames T. Johnson18-Jun-02 19:27 

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.