RichTextBox.Paste(DataFormats+Format) 方法

定義

以指定的剪貼簿格式貼上剪貼簿的內容。

public:
 void Paste(System::Windows::Forms::DataFormats::Format ^ clipFormat);
public void Paste (System.Windows.Forms.DataFormats.Format clipFormat);
override this.Paste : System.Windows.Forms.DataFormats.Format -> unit
Public Sub Paste (clipFormat As DataFormats.Format)

參數

clipFormat
DataFormats.Format

剪貼簿格式,其中的資料應從剪貼簿取得。

範例

下列程式碼範例示範如何使用 Paste 方法將點陣圖貼到 RichTextBox 控制項中。 從檔案開啟點陣圖之後,此範例會 SetDataObject 使用 方法將點陣圖複製到 Windows 剪貼簿。 最後,此範例會擷取 物件的格式 Bitmap 、確認格式可以貼到 控制項中,並使用 Paste 方法來貼上 RichTextBox 資料。

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

備註

您可以使用這個方法,將資料從剪貼簿貼到 控制項。 這個版本的 Paste 方法與 方法不同 TextBoxBase.Paste ,因為它可讓您只貼上指定剪貼簿格式的文字。 您可以使用 CanPaste 方法來判斷剪貼簿中的資料是否為指定的剪貼簿格式。 然後,您可以呼叫這個版本的 Paste 方法,以確保貼上作業是以適當的資料格式進行。

適用於

另請參閱