Geekpedia Tutorials Home

Building a C# Chat Client and Server

Building a C# Chat Client and ServerA step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.

in C# Programming Tutorials

Getting Hard Drive Information

Getting Hard Drive InformationA C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.

in C# Programming Tutorials

UPS Shipping Calculator

UPS Shipping CalculatorThis tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.

in PHP Programming Tutorials

Create Your Own Rich Text Editor

Create Your Own Rich Text EditorCreating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.

in JavaScript Programming Tutorials
Search
Tutorials
Programming Tutorials
IT Jobs
From CareerBuilder

Clipboard Copy and Paste with C#

Learn how to copy and paste text and images from and to the Windows clipboard using the Clipboard class in C# and .NET 2.0. You will also see how to clear the content of the clipboard.

On Sunday, June 4th 2006 at 03:38 AM
By Andrew Pociu (View Profile)
*****   (Rated 4.1 with 59 votes)
Contextual Ads
More C# Resources
Advertisement
Download this Visual Studio 2005 project Download this project (Visual Studio 2005)

This simple tutorial will show you how to access the Windows clipboard in .NET 2.0 using C#. Start by creating a Windows Application inside Visual Studio 2005 and to the form add a TextBox (txtClipboard), a PictureBox (picClipboard) andfive buttons (btnCopyText, btnPasteText, btnCopyImage, btnPasteImage, btnClear). Also add a OpenFileDialog object and name it opnImage. The form elements should be arranged so that it looks similar to the one below:

Clipboard Manager Form

As you can see here, the functionality of copying and pasting text is separated from the one of copying and pasting images, and it makes sense, since you can't always paste images in the same place you are pasting text, unless you are using an advanced container that supports both types of content, such as Microsoft Word. Since in our application we don't have a control where both images and text can be pasted, we are using a TextBox for the text, and a PictureBox for the images.

The first functionality to be added is copying text to the clipboard. Double click the btnCopyText button and you will get to its Click event. Inside it there is only one line to add:


Clipboard.SetText(txtClipboard.Text);


We are using the SetText() method to set the clipboard to the text inside the txtClipboard TextBox.
Now it's time to paste, thus double click the btnPasteText button and once you get to its Click event, use the following line:


txtClipboard.Text = Clipboard.GetText();


Here the TextBox is being set to the content of the clipboard (as long as the content is plain text).

When copying an image, we first need to allow the user to pick an image, and here's where the OpenFileDialog object comes into action. Double click the btnCopyImage button so that Visual Studio 2005 can create its Click event, and use the following piece of code:


if (opnImage.ShowDialog() == DialogResult.OK)

{

Image imgToCopy = Image.FromFile(opnImage.FileName);

Clipboard.SetImage(imgToCopy);

}


First we check to see if the user has clicked OK and thus selected a file. If he did, we create an Image object from that file. Please note that at this point, in a real-life application, you will want to allow the user to only select a number of file types (GIF, JPEG, PNG, etc.), and also check after the selection was made and before it is put into the clipboard, if a valid image file was selected.
After the Image object is created by passing the path of the image file, we put this object into the clipboard.

Now double click the btnPasteImage button and inside the Click event, we're going to put a line of code that pastes the content of the clipboard into a PictureBox. Pasting images is very similar to pasting text, only that now we use the GetImage() method instead of GetText() and for the container we use a PictureBox control instead of a TextBox control:


picClipboard.Image = Clipboard.GetImage();


As for clearing the clipboard, it couldn't get any easier. If you double click the btnClear button and use the following line of code inside the btnClear_Click event, you are done:


Clipboard.Clear();


Thanks to the Clipboard class, this tutorial was a very easy one. If you wish to download the code as a Visual Studio 2005 C# Solution, use the link at the top of the tutorial.
Digg Digg It!     Del.icio.us Del.icio.us     Reddit Reddit     StumbleUpon StumbleIt     Newsvine Newsvine     Furl Furl     BlinkList BlinkList

Rate Rate this tutorial
Comment Current Comments
by Darius Collins on Thursday, June 8th 2006 at 04:33 AM

Your Download this project link downloads the solution for the Download program manager not the Clipboard class tutorial!

by Andrei Pociu on Thursday, June 8th 2006 at 05:19 AM

Thanks for pointing that out. The link is fixed.

by Samir on Monday, June 26th 2006 at 08:43 AM

Hi,
Andrei Pociu

Thanks for this article.This realy helped me.

by shakti on Monday, July 10th 2006 at 06:02 AM

Thks a lot!! It helped me.

by Attila Turóczy on Friday, July 28th 2006 at 01:49 PM

Real good!

by Paul on Wednesday, February 21st 2007 at 12:30 AM

and how copy both to paste in a word document?

by Raja Singh on Friday, April 27th 2007 at 08:03 AM

How to copy and paste Ms Word table on the picturebox (like Image)

by Nekrogamia on Tuesday, June 5th 2007 at 03:59 PM

How I do this if I`m using a webbrowser?

create a copy, cut and paste buttons that copy my selection on webbrowser ?

thanks, and sorry for my bad inglish, i`m a brazilian.

by ranjuan on Saturday, June 16th 2007 at 09:32 AM

Is there a possibilty to paste text into a notepad process using c#?

by Ruwan Gunawardhana on Saturday, August 11th 2007 at 08:18 AM

I find.... Find find......................find ohhh...! ............ :-) :-) :) I found.....!!!! THANK YOU..........!!!

by jitendra on Tuesday, September 11th 2007 at 03:35 AM

when i m going to click on to copy text button than i got an 'System.ArgumentNullException' type of error

by haxxor on Saturday, November 10th 2007 at 09:31 PM

This is a great application for someone new to programing like me but I belive you might have accidentally mixed up diffrent commands.
Clipboard.Clear(); code must be changed to something like
txtClipboard.Clear(); so then it will know it must clear all on the text box. once again thanks a ton im going to add a version of this to my text program i have been working on.

by shifty on Saturday, November 10th 2007 at 10:13 PM

Clipboard.Clear() clears the content of the clipboard, so it should work fine.

by tomas on Thursday, March 13th 2008 at 05:08 AM

When copying an image with transparent background (eg .png) the transparent background gets blue. Any solution to that?

by christian on Wednesday, April 2nd 2008 at 03:50 AM

by Rish on Saturday, April 19th 2008 at 05:09 AM

I am trying to copy my own data type (a class which I have created in my application), so I use the Clipboard.SetData() and GetData() methods. The problem is that when I do GetData() the resulting object is null. Strangely enough, the ContainsData() method returns true when I give the type string for my class.

The code I\'m using is:

for copying:

MyOwnClass clipdata = new MyOwnClass();
Clipboard.SetData(typeof(MyOwnClass).FullName, clipdata);

for pasting:

if (Clipboard.ContainsData(typeof(MyOwnClass).FullName))
{
MyOwnClass clipdata = (MyOwnClass)Clipboard.GetData(typeof(MyOwnClass).FullName);
}

by James on Monday, April 28th 2008 at 04:38 PM

You can only use SetData with serializable objects. objects that implement the ISerializable interface. no exception is thrown for passing objects that do not have this interface and it still says it has the data through ContainsData(), cause it does, it just doesn't know how to get it out.

by HH on Friday, June 20th 2008 at 02:51 AM

Very helpful

by dharn on Thursday, July 3rd 2008 at 02:28 AM

Thanks for ur code it looks really simple n easily understandable

by dongkil on Monday, August 11th 2008 at 12:24 AM

thx, it's helpful

by Rana on Thursday, September 11th 2008 at 09:22 AM

Thanks Dude :)
Very simple and precise explanation.
Very Helpful..

by D on Friday, December 5th 2008 at 04:39 PM

Awesome introduction to the clipboard for us beginners.

Thanks

by Ross Brigoli on Thursday, March 19th 2009 at 02:25 AM

Hi,

Does this work if the .NET windows application is accessed thru Citrix?

If so, which clipboard will it affect? Is it the clipboard of the Citrix server or the local client machine?

you reponse will be very helpful.


Thanks
-Ross

by Ross Brigoli on Thursday, March 19th 2009 at 02:25 AM

Hi,

Does this work if the .NET windows application is accessed thru Citrix?

If so, which clipboard will it affect? Is it the clipboard of the Citrix server or the local client machine?

you reponse will be very helpful.


Thanks
-Ross

by ion on Tuesday, April 14th 2009 at 09:23 PM

thanks

by Jon on Tuesday, April 21st 2009 at 08:16 PM

by ranjuan on Saturday, June 16th 2007 at 09:32 AM
Is there a possibilty to paste text into a notepad process using c#?


yes, you can use SendKeys.Send() or SendKeys.SendWait() methods

by Nick on Sunday, August 9th 2009 at 11:59 AM

what using statements are you guys using???

by Mina on Saturday, September 19th 2009 at 04:55 PM

This was quite usefull, i'm working on creating a program that frees the memory, if you cut and paste alot of things with a high amount of bytes it'll slow down the computer, clearing the clipboard will surely speed things up :-)!

by Pootle on Thursday, October 15th 2009 at 11:33 AM

>by Nick on Sunday, August 9th 2009 at 11:59 AM
>what using statements are you guys using???

Clipboard is in System.Windows.Forms.

by youtubeline on Thursday, December 24th 2009 at 08:13 AM

thanks a lot

by ASIM on Thursday, April 8th 2010 at 03:20 AM

hmmm...... gud application

by GMan on Thursday, April 15th 2010 at 07:44 PM

Hi,
I got a Embeded file in a word document (Insert-->File). And I want to save that to a file to the disk. So far I copied the InlineShape to the clipboard. Then used following to get the file.


IDataObject data = Clipboard.GetDataObject();
object fileobj;

if (data.GetDataPresent(DataFormats.EnhancedMetafile))
{
fileobj = data.GetData(DataFormats.EnhancedMetafile);

but last line throws an Exception

If I go to my windows explored and right click and paste, the file saves as a file. So I know the file has copied to the clipboard. But how to save that using code?
Any ideas?
Cheers,
GMan

by John on Wednesday, September 15th 2010 at 10:21 AM

Hi,
I would like to know how to get more clipboard information like the Application that the data come from.

Any ideas?

Thanks

by $@#$ on Thursday, October 21st 2010 at 09:51 AM

hello

by Mushy on Thursday, November 11th 2010 at 12:29 AM

hi
can sme1 plz help me :(
i need to find a way to get the data on clipboard
the prob is the data on clipboard is a table
n i dunno hw to get zat table

wat i need to do is as follows:
i hve hve to get the table on clipboard without any formating it had earlier, like bold color n so on

plz help
thks

by Golden Man on Thursday, March 24th 2011 at 02:57 PM

Great Thanks !!!

by Osama on Wednesday, April 13th 2011 at 08:45 AM

thnx

by dating sites on Sunday, November 20th 2011 at 06:34 AM

regardless of being too much harm, but now they should be recovered, but, in his role of the source, you probably also have the energy should be cleared it.

by best balance transfer credit cards on Monday, December 12th 2011 at 05:30 AM

Use System.Windows.Forms.Cursor.Position to get the cursor position in screen coordinates.I admire the important information you offer within your content. I'll bookmark your web site and have my kids examine up the following typically.

by balance transfer credit card on Monday, December 12th 2011 at 05:31 AM

Use System.Windows.Forms.Cursor.Position to get the cursor position in screen coordinates.I admire the important information you offer within your content. I'll bookmark your web site and have my kids examine up the following typically.

by GenF20 Plus on Tuesday, December 20th 2011 at 03:35 AM

Thanks a lot for providing individuals with a very wonderful possiblity to discover important secrets from this web site. It's always so enjoyable and as well , stuffed with a great time for me personally and my office colleagues to search your website at the least three times in a week to read the new stuff you have. And lastly, I'm so always fascinated with all the magnificent suggestions you give. Certain 4 tips in this article are particularly the very best we've ever had.

by Human Growth Hormone on Tuesday, December 20th 2011 at 03:37 AM

Thanks a lot for providing individuals with a very wonderful possiblity to discover important secrets from this web site. It's always so enjoyable and as well , stuffed with a great time for me personally and my office colleagues to search your website at the least three times in a week to read the new stuff you have. And lastly, I'm so always fascinated with all the magnificent suggestions you give. Certain 4 tips in this article are particularly the very best we've ever had.

by Phen375 on Tuesday, December 20th 2011 at 03:46 AM

Thanks a lot for providing individuals with a very wonderful possiblity to discover important secrets from this web site. It's always so enjoyable and as well , stuffed with a great time for me personally and my office colleagues to search your website at the least three times in a week to read the new stuff you have. And lastly, I'm so always fascinated with all the magnificent suggestions you give. Certain 4 tips in this article are particularly the very best we've ever had.

by Best Diet Pills on Tuesday, December 20th 2011 at 03:53 AM

Thanks a lot for providing individuals with a very wonderful possiblity to discover important secrets from this web site. It's always so enjoyable and as well , stuffed with a great time for me personally and my office colleagues to search your website at the least three times in a week to read the new stuff you have. And lastly, I'm so always fascinated with all the magnificent suggestions you give. Certain 4 tips in this article are particularly the very best we've ever had.

by Teeth Whitening on Tuesday, December 20th 2011 at 03:57 AM

Thanks a lot for providing individuals with a very wonderful possiblity to discover important secrets from this web site. It's always so enjoyable and as well , stuffed with a great time for me personally and my office colleagues to search your website at the least three times in a week to read the new stuff you have. And lastly, I'm so always fascinated with all the magnificent suggestions you give. Certain 4 tips in this article are particularly the very best we've ever had.

by Pure Acai Berry Max on Tuesday, December 20th 2011 at 03:59 AM

Thanks a lot for providing individuals with a very wonderful possiblity to discover important secrets from this web site. It's always so enjoyable and as well , stuffed with a great time for me personally and my office colleagues to search your website at the least three times in a week to read the new stuff you have. And lastly, I'm so always fascinated with all the magnificent suggestions you give. Certain 4 tips in this article are particularly the very best we've ever had.

by GenFX on Tuesday, December 20th 2011 at 04:04 AM

Thanks a lot for providing individuals with a very wonderful possiblity to discover important secrets from this web site. It's always so enjoyable and as well , stuffed with a great time for me personally and my office colleagues to search your website at the least three times in a week to read the new stuff you have. And lastly, I'm so always fascinated with all the magnificent suggestions you give. Certain 4 tips in this article are particularly the very best we've ever had.

by HGH Advanced on Tuesday, December 20th 2011 at 04:04 AM

Thanks a lot for providing individuals with a very wonderful possiblity to discover important secrets from this web site. It's always so enjoyable and as well , stuffed with a great time for me personally and my office colleagues to search your website at the least three times in a week to read the new stuff you have. And lastly, I'm so always fascinated with all the magnificent suggestions you give. Certain 4 tips in this article are particularly the very best we've ever had.

by Capsiplex on Tuesday, December 20th 2011 at 04:09 AM

Thanks a lot for providing individuals with a very wonderful possiblity to discover important secrets from this web site. It's always so enjoyable and as well , stuffed with a great time for me personally and my office colleagues to search your website at the least three times in a week to read the new stuff you have. And lastly, I'm so always fascinated with all the magnificent suggestions you give. Certain 4 tips in this article are particularly the very best we've ever had.

by Meratol on Tuesday, December 20th 2011 at 04:10 AM

Thanks a lot for providing individuals with a very wonderful possiblity to discover important secrets from this web site. It's always so enjoyable and as well , stuffed with a great time for me personally and my office colleagues to search your website at the least three times in a week to read the new stuff you have. And lastly, I'm so always fascinated with all the magnificent suggestions you give. Certain 4 tips in this article are particularly the very best we've ever had.

by Proactol Plus on Tuesday, December 20th 2011 at 04:25 AM

Thanks a lot for providing individuals with a very wonderful possiblity to discover important secrets from this web site. It's always so enjoyable and as well , stuffed with a great time for me personally and my office colleagues to search your website at the least three times in a week to read the new stuff you have. And lastly, I'm so always fascinated with all the magnificent suggestions you give. Certain 4 tips in this article are particularly the very best we've ever had.

by Nuratrim on Tuesday, December 20th 2011 at 04:28 AM

Thanks a lot for providing individuals with a very wonderful possiblity to discover important secrets from this web site. It's always so enjoyable and as well , stuffed with a great time for me personally and my office colleagues to search your website at the least three times in a week to read the new stuff you have. And lastly, I'm so always fascinated with all the magnificent suggestions you give. Certain 4 tips in this article are particularly the very best we've ever had.

by HGHAdvanced on Tuesday, December 20th 2011 at 02:45 PM

Thanks a lot for providing individuals with a very wonderful possiblity to discover important secrets from this web site. It's always so enjoyable and as well , stuffed with a great time for me personally and my office colleagues to search your website at the least three times in a week to read the new stuff you have. And lastly, I'm so always fascinated with all the magnificent suggestions you give. Certain 4 tips in this article are particularly the very best we've ever had.

by on Saturday, January 14th 2012 at 01:53 AM

Once you have recreated the problem and captured these steps, you can save them to a file and send it to your support person, who can then open it up and view

by Waclawa on Saturday, January 14th 2012 at 01:55 AM

Once you have recreated the problem and captured these steps, you can save them to a file and send it to your support person, who can then open it up and view

by Manjeet Sharma on Wednesday, January 25th 2012 at 02:43 AM

How to make document management system software in .Net window application using c#

by bookworm game on Tuesday, October 2nd 2012 at 02:53 AM

For example: we need an integer input, but an string was entered... how to repeat the try -

by emergency weather radio iphone app on Thursday, October 25th 2012 at 02:45 AM

OpenFileDialog object and name it opnImage. The form elements should be arranged so that it looks similar to the one below:


Comment Comment on this tutorial
Name: Email:
Message:
Comment Related Tutorials
There are no related tutorials.

Comment Related Source Code
There is no related source code.

Jobs C# Job Search
My skills include:
Enter a City:

Select a State:


Advanced Search >>
Ads

From the creators of Geekpedia, a revolutionary new coupon website!

BargainEZ has coupons codes, printable coupons, bargains and it is the leading source of Passbook coupons for iPhone and iPod touch devices.

Coupons
Discover Geekpedia
Other Resources