RichTextBox.CanPaste(DataFormats+Format) 方法

定义

确定是否可以粘贴指定数据格式的剪贴板信息。

public:
 bool CanPaste(System::Windows::Forms::DataFormats::Format ^ clipFormat);
public bool CanPaste (System.Windows.Forms.DataFormats.Format clipFormat);
member this.CanPaste : System.Windows.Forms.DataFormats.Format -> bool
Public Function CanPaste (clipFormat As DataFormats.Format) As Boolean

参数

clipFormat
DataFormats.Format

DataFormats.Format 值之一。

返回

如果可以粘贴指定数据格式的剪贴板数据,则为 true;否则为 false

示例

下面的代码示例演示如何使用 Paste 方法将位图粘贴到 控件中 RichTextBox 。 从 文件打开位图后,该示例使用 SetDataObject 方法将位图复制到 Windows 剪贴板。 最后,该示例检索 对象的格式 Bitmap ,使用 CanPaste 方法验证格式是否可以粘贴到 RichTextBox 控件中,然后使用 Paste 方法粘贴数据。

private:
   bool pasteMyBitmap( String^ fileName )
   {
      // Open an bitmap from file and copy it to the clipboard.
      Bitmap^ myBitmap = gcnew 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;
      }
   }
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;
    }
}
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

注解

在允许用户将信息粘贴到 RichTextBox 控件之前,可以使用此方法确定剪贴板的当前内容是否为指定的剪贴板数据格式。 例如,可以为粘贴命令MenuItem的事件创建事件处理程序Popup,并使用此方法确定是否应根据剪贴板中的数据类型启用粘贴MenuItem

适用于

另请参阅