Title: Copy and paste text to and from the clipboard C#
Using the clipboard to copy and paste text is simple. Use the Clipboard object's SetText method to copy text to the clipboard as in the following code.
// Copy text to the clipboard.
private void btnCopy_Click(object sender, EventArgs e)
{
Clipboard.SetText(txtCopy.Text);
}
Use the Clipboard object's GetText method to retrieve the text that's in the clipboard as in the following code.
// Paste text from the clipboard.
private void btnPaste_Click(object sender, EventArgs e)
{
txtPaste.Text = Clipboard.GetText();
}
That's all there is to it! In later posts I'll show how you can copy and paste other kinds of data such as images or objects of your own creation.
Download the example to experiment with it and to see additional details.
|