RichTextBox.Paste Method (DataFormats.Format)

Pastes the contents of the Clipboard in the specified Clipboard format.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

public void Paste(
	DataFormats.Format clipFormat
)

Parameters

clipFormat
Type: System.Windows.Forms.DataFormats.Format

The Clipboard format in which the data should be obtained from the Clipboard.

You can use this method to paste data from the clipboard into the control. This version of the Paste method is different from the TextBoxBase.Paste method as it enables you to paste only text in a specified Clipboard format. You can use the CanPaste method to determine whether the data within the Clipboard is in the specified Clipboard format. You can then call this version of the Paste method to ensure that the paste operation is made with the appropriate data format.

The following code example demonstrates how to use the Paste method to paste a bitmap into the RichTextBox control. After opening a bitmap from file, the example uses the SetDataObject method to copy the bitmap to the Windows clipboard. Finally, the example retrieves the format for the Bitmap object, verifies that the format can be pasted into the RichTextBox control, and uses the Paste method to paste the data.

private bool pasteMyBitmap(string fileName)
{

	// Open an bitmap from file and copy it to the clipboard.
	Bitmap myBitmap = new Bitmap(fileName);
			
	// Copy the bitmap to the clipboard.
	Clipboard.SetDataObject(myBitmap);

	// Get the format for the object type.
	DataFormats.Format myFormat = DataFormats.GetFormat(DataFormats.Bitmap);

	// After verifying that the data can be pasted, paste it. 
	if(richTextBox1.CanPaste(myFormat))
	{
		richTextBox1.Paste(myFormat);
		return true;
	}
	else
	{
		MessageBox.Show("The data format that you attempted to paste is not supported by this control.");
		return false;
	}
}

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft