Serialcoder en Français Serialcoder in English
TEL : +33 (0)9 72 13 15 17

Windows Forms FAQ resources

18. Windows Forms Tips

18.16 How do I use the system clipboard?


The SetDataObject and GetDataObject methods in the Clipboard class found in the System.Windows.Forms namespace allows you to access the clipboard. Here is some code.

     string text = "Some text for the clipboard";
     Clipboard.SetDataObject(text); //clipboard now has "Some text for the clipboard"

     text = ""; //zap text so it can be reset...
     IDataObject data = Clipboard.GetDataObject();
     if(data.GetDataPresent(DataFormats.Text))
     {
          text = (String)data.GetData(DataFormats.Text);
          //text is now back to "Some text for the clipboard"
     }