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 控制項中。 例如,您可以為 Popup 貼上命令 MenuItem 的事件建立事件處理常式,並使用這個方法來判斷是否應該根據剪貼簿中的資料類型啟用貼 MenuItem 上。

適用於

另請參閱