RichTextBox.CanPaste(DataFormats+Format) Metode
Definisi
Penting
Beberapa informasi terkait produk prarilis yang dapat diubah secara signifikan sebelum dirilis. Microsoft tidak memberikan jaminan, tersirat maupun tersurat, sehubungan dengan informasi yang diberikan di sini.
Menentukan apakah Anda dapat menempelkan informasi dari Clipboard dalam format data yang ditentukan.
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
Parameter
- clipFormat
- DataFormats.Format
Salah DataFormats.Format satu nilai.
Mengembalikan
true
jika Anda dapat menempelkan data dari Clipboard dalam format data yang ditentukan; jika tidak, false
.
Contoh
Contoh kode berikut menunjukkan cara menggunakan Paste metode untuk menempelkan bitmap ke RichTextBox dalam kontrol. Setelah membuka bitmap dari file, contohnya menggunakan SetDataObject metode untuk menyalin bitmap ke clipboard Windows. Terakhir, contoh mengambil format untuk Bitmap objek , menggunakan CanPaste metode untuk memverifikasi bahwa format dapat ditempelkan ke dalam RichTextBox kontrol, lalu menggunakan Paste metode untuk menempelkan data.
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
Keterangan
Anda dapat menggunakan metode ini untuk menentukan apakah isi Papan Klip saat ini berada dalam format data Clipboard tertentu sebelum mengaktifkan pengguna untuk menempelkan informasi ke RichTextBox dalam kontrol. Misalnya, Anda dapat membuat penanganan aktivitas untuk Popup peristiwa perintah MenuItem tempel dan menggunakan metode ini untuk menentukan apakah tempel MenuItem harus diaktifkan berdasarkan jenis data di Clipboard.