RichTextBox.CanPaste(DataFormats+Format) Método

Definición

Determina si se puede pegar información desde el Portapapeles en el formato de datos especificado.

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

Parámetros

clipFormat
DataFormats.Format

Uno de los valores de DataFormats.Format.

Devoluciones

Es true si se pueden pegar datos desde el Portapapeles en el formato especificado; de lo contrario, es false.

Ejemplos

En el ejemplo de código siguiente se muestra cómo usar el Paste método para pegar un mapa de bits en el RichTextBox control . Después de abrir un mapa de bits desde el archivo, en el ejemplo se usa el SetDataObject método para copiar el mapa de bits en el Portapapeles de Windows. Por último, el ejemplo recupera el formato del Bitmap objeto , usa el CanPaste método para comprobar que el formato se puede pegar en el RichTextBox control y, a continuación, usa el Paste método para pegar los datos.

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

Comentarios

Puede usar este método para determinar si el contenido actual del Portapapeles está en un formato de datos del Portapapeles especificado antes de permitir que el usuario pegue la información en el RichTextBox control. Por ejemplo, podría crear un controlador de eventos para un evento de un Popup comando MenuItem paste y usar este método para determinar si el pegado MenuItem debe habilitarse en función del tipo de datos del Portapapeles.

Se aplica a

Consulte también