Export (0) Print
Expand All

RichTextBox.CanPaste Method

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

[Visual Basic]
Public Function CanPaste( _
   ByVal clipFormat As DataFormats.Format _
) As Boolean
[C#]
public bool CanPaste(
 DataFormats.Format clipFormat
);
[C++]
public: bool CanPaste(
 DataFormats.Format* clipFormat
);
[JScript]
public function CanPaste(
   clipFormat : DataFormats.Format
) : Boolean;

Parameters

clipFormat
One of the System.Windows.Forms.DataFormats.Format values.

Return Value

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

Remarks

You can use this method to determine whether the current contents of the Clipboard are in a specified Clipboard data format before allowing 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.

Example

[Visual Basic, C#, C++] The following 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.

[Visual Basic] 
Private Function PasteMyBitmap(ByVal Filename As String) As Boolean

    'Open an bitmap from file and copy it to the clipboard.
    Dim MyBitmap As Bitmap
    MyBitmap = Bitmap.FromFile(Filename)

    ' Copy the bitmap to the clipboard.
    Clipboard.SetDataObject(MyBitmap)

    ' Get the format for the object type.
    Dim MyFormat As DataFormats.Format = DataFormats.GetFormat(DataFormats.Bitmap)

    ' After verifying that the data can be pasted, paste it.
    If RichTextBox1.CanPaste(MyFormat) Then

        RichTextBox1.Paste(MyFormat)
        PasteMyBitmap = True

    Else

        MessageBox.Show("The data format that you attempted to paste is not supported by this control.")
        PasteMyBitmap = False

    End If


End Function

[C#] 
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;
    }
}


[C++] 
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(S"The data format that you attempted to paste is not supported by this control.");
            return false;
        }
    }

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

RichTextBox Class | RichTextBox Members | System.Windows.Forms Namespace | Paste | System.Windows.Forms.DataFormats.Format

Show:
© 2014 Microsoft