Search This Blog

Sunday, March 9, 2014

Copy and Paste on Clipboard using C#

The Clipboard class provides methods to save and retrieve data from system clipboard.

Clipboard has static methods to copy and paste data. The SetDataObject method is used to store data that is in object format on the clipboard. The following code snippet copies selected text from a TextBox control to the clipboard.

Clipboard.SetDataObject(TextBox1.SelectedText); 


The GetDataObject method gets the data that is stored on the clipboard. The following code snippet gets data from the clipboard and displays in a Label control. 


IDataObject iData = Clipboard.GetDataObject();
// Is Data Text?
if (iData.GetDataPresent(DataFormats.Text))
        label1.Text = (String)iData.GetData(DataFormats.Text);
else
    label1.Text = "Data not found."

Clear Clipboard

Clear method removes all data from the Clipboard.
Clipboard.Clear();


No comments:

Post a Comment