A 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.
A C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.
This 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.
Creating 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.
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) |
||
|
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: 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 It!
Del.icio.us
Reddit
StumbleIt
Newsvine
Furl
BlinkList
|
|||
|
|||
Current CommentsYour Download this project link downloads the solution for the Download program manager not the Clipboard class tutorial!
Thanks for pointing that out. The link is fixed.
Hi,
Andrei Pociu
Thanks for this article.This realy helped me.
Thks a lot!! It helped me.
Real good!
and how copy both to paste in a word document?
How to copy and paste Ms Word table on the picturebox (like Image)
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.
Is there a possibilty to paste text into a notepad process using c#?
I find.... Find find......................find ohhh...! ............ :-) :-) :) I found.....!!!! THANK YOU..........!!!
when i m going to click on to copy text button than i got an 'System.ArgumentNullException' type of error
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.
Clipboard.Clear() clears the content of the clipboard, so it should work fine.
When copying an image with transparent background (eg .png) the transparent background gets blue. Any solution to that?
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);
}
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.
Very helpful
Thanks for ur code it looks really simple n easily understandable
thx, it's helpful
Thanks Dude :)
Very simple and precise explanation.
Very Helpful..
Awesome introduction to the clipboard for us beginners.
Thanks
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
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
thanks
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
what using statements are you guys using???
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 Nick on Sunday, August 9th 2009 at 11:59 AM
>what using statements are you guys using???
Clipboard is in System.Windows.Forms.
thanks a lot
hmmm...... gud application
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
Hi,
I would like to know how to get more clipboard information like the Application that the data come from.
Any ideas?
Thanks
hello
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
Great Thanks !!!
thnx
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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
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
How to make document management system software in .Net window application using c#
For example: we need an integer input, but an string was entered... how to repeat the try -
OpenFileDialog object and name it opnImage. The form elements should be arranged so that it looks similar to the one below:
Related Tutorials
Related Source Code
C# Job SearchFrom 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.