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.
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 60 votes)
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:
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:
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);
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:
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|
Rate this tutorial
There are no related tutorials.
Related Source Code
There is no related source code.
C# Job Search