c# copy to clipboard with example

[sam_ad id=”48″ codes=”true”]Hi Guys,

So lately I’ve been working on some c# project not big one’s because I only like something that has something to do with web. So in c# my projects are more to do with public available APIs.

So the project I’ve been working on now I thought it would be a good Idea to allow user of one of my util to copy a URL to a clipboard.

So how? Luckily for me Microsoft .NET framework ships a class called Clipboard

How to use this class is what this post is all about. I will be covering the basic usage only.

I find it better to show it creating a simple Form application.

Lets get started. You can download the sample project files at the end.

I created this simple form

 

This form as you notice does have three actions

  1. Copy to clipboard
  2. Clear clipboard
  3. Launch notepad

Clipboard related code that gets executed when you click on Copy to clipboard is shown below

As you can see that we are using Clipboard class and calling a static method called SetText. Above code can be read as copy what I type to clipboard.

So when you launch notepad then you can paste your copied text there by pressing CTRL+C

To Clear the clipboard, this is how  you do it

[wpdm_file id=11]

Likewise you can add Images, data objects etc to clipboard. You can read more about Clipboard class here

http://msdn.microsoft.com/en-us/library/system.windows.forms.clipboard.aspx

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.