Click here to Skip to main content
15,879,535 members
Articles / Desktop Programming / Windows Forms

Set and Get Text in the Clipboard

Rate me:
Please Sign up or sign in to vote.
4.18/5 (6 votes)
5 Oct 2009CPOL1 min read 61.3K   932   22   10
How to set and get text in the Clipboard using C#.

Introduction

In this article, I will explain how to manipulate text in the Clipboard. Here is how the main form in the sample application looks like:

Image 1

Adding Controls

First of all, create a new project (Windows Forms Application) and add two Labels, then add a TextBox (textClip), and then another Label (labelClip) that will show what is in the Clipboard. Now, add a Timer (timerRefresh) that will refresh what is in the Clipboard to you automatically, and finally, add a Button (buttonSet).

The Code

First of all, define the interval of the Timer. In this article, I will use 5000ms (5 sec.) as interval. Now, let's get into the code!

C#
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        PasteData();
    }

    private void SetToClipboard(object text)
    {
        System.Windows.Forms.Clipboard.SetDataObject(text);
    }

    private void PasteData()
    {
        IDataObject ClipData = System.Windows.Forms.Clipboard.GetDataObject();
        timerRefresh.Start();
        if (ClipData.GetDataPresent(DataFormats.Text))
        {
           string s = System.Windows.Forms.Clipboard.GetData(DataFormats.Text).ToString();
            labelClip.Text = s;
        }
    }

    private void buttonSet_Click(object sender, EventArgs e)
    {
        SetToClipboard(textClip.Text);
    }

    private void timerRefresh_Tick(object sender, EventArgs e)
    {
        PasteData();
    }
}

Explanation

  • PasteData(): In the PasteData, we have a IDataObject called ClipData that will store the Clipboard data object, then the Timer starts. The if clause will see if the Clipboard has something; if it has, it will store the data in a string(s), for use later to change the text of labelClip.
  • timerRefresh_Tick(object sender, EventArgs e): In this function, every time the Timer reaches the interval time (5 sec.), it will execute PasteData.
  • private void SetToClipboard(object text): Here, we will add a text that is in the object test to the Clipboard, using SetDataObject.
  • private void buttonSet_Click(object sender, EventArgs e): When the user clicks in the buttonSet button, it will use SetToClipboard, using the text that is in testClip as the variable.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
CEO
Brazil Brazil
I develop for mobiles since 2005, using J2ME for those old color mobile phones(like the good Nokia ones), after this at 2006 I've started on WIndows Mobile development using VB and some C#, at 2007 I was totally focused on Windows Mobile development, developing things like Calendars, simple games and a fully-featured text editor. Aug 2008 I moved to Symbian S60 3rd development using Python and C++. After that I've started on the platform that I like most: Android. One week ago I bought my first Mac, so I was going to start on iOS development, but I saw that it's so much difficult(Objective-C is the problem)

Comments and Discussions

 
GeneralMy vote of 5 Pin
Gun Gun Febrianza9-Jan-17 11:14
Gun Gun Febrianza9-Jan-17 11:14 
Questionthanks for sharing Pin
Gun Gun Febrianza8-Jan-14 1:26
Gun Gun Febrianza8-Jan-14 1:26 
GeneralIs this a joke? Pin
Groulien28-Apr-11 0:00
Groulien28-Apr-11 0:00 
GeneralMy vote of 1 Pin
NeoPunk7-Oct-09 0:41
NeoPunk7-Oct-09 0:41 
Awful article that should be deleted. This article is a part of MSDN http://msdn.microsoft.com/en-us/library/system.windows.forms.clipboard(VS.71).aspx
GeneralIt's awful article Pin
NeoPunk7-Oct-09 0:40
NeoPunk7-Oct-09 0:40 
GeneralRe: It's awful article Pin
Nathan Campos7-Oct-09 6:24
Nathan Campos7-Oct-09 6:24 
GeneralUse of timer Pin
Md. Marufuzzaman5-Oct-09 20:45
professionalMd. Marufuzzaman5-Oct-09 20:45 
GeneralDon't forget Pin
The_Mega_ZZTer5-Oct-09 14:35
The_Mega_ZZTer5-Oct-09 14:35 
GeneralRe: Don't forget Pin
Lyon Sun6-Oct-09 4:14
Lyon Sun6-Oct-09 4:14 
GeneralRe: Don't forget Pin
Nathan Campos6-Oct-09 6:04
Nathan Campos6-Oct-09 6:04 

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.