RichTextBox.CanPaste(DataFormats+Format) Method

Definition

Determines whether you can paste information from the Clipboard in the specified data format.

public bool CanPaste (System.Windows.Forms.DataFormats.Format clipFormat);

Parameters

clipFormat
DataFormats.Format

One of the DataFormats.Format values.

Returns

true if you can paste data from the Clipboard in the specified data format; otherwise, false.

Examples

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, uses the CanPaste method to verify that the format can be pasted into the RichTextBox control, and then 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;
    }
}

Remarks

You can use this method to determine whether the current contents of the Clipboard are in a specified Clipboard data format before enabling the user to paste the information into the RichTextBox control. For example, you could create an event handler for a Popup event of a paste command MenuItem and use this method to determine whether the paste MenuItem should be enabled based on the type of data in the Clipboard.

Applies to

Product Versions
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

See also