TextBox.Paste Kejadian

Definisi

Terjadi ketika teks ditempelkan ke dalam kontrol.

public:
 virtual event TextControlPasteEventHandler ^ Paste;
// Register
event_token Paste(TextControlPasteEventHandler const& handler) const;

// Revoke with event_token
void Paste(event_token const* cookie) const;

// Revoke with event_revoker
TextBox::Paste_revoker Paste(auto_revoke_t, TextControlPasteEventHandler const& handler) const;
public event TextControlPasteEventHandler Paste;
function onPaste(eventArgs) { /* Your code */ }
textBox.addEventListener("paste", onPaste);
textBox.removeEventListener("paste", onPaste);
- or -
textBox.onpaste = onPaste;
Public Custom Event Paste As TextControlPasteEventHandler 
<TextBox Paste="eventhandler"/>

Jenis Acara

Contoh

Contoh ini memperlihatkan cara menangani peristiwa Tempel untuk mengganti pemisah baris dengan koma saat menempelkan ke bidang alamat. Jika tidak, menempelkan alamat yang disalin dari beberapa baris akan menyebabkan kehilangan data.

<TextBox Header="Address" Paste="AddressTextBox_Paste"/>
private async void AddressTextBox_Paste(object sender, TextControlPasteEventArgs e)
{
    TextBox addressBox = sender as TextBox;
    if (addressBox != null)
    {
        // Mark the event as handled first. Otherwise, the
        // default paste action will happen, then the custom paste
        // action, and the user will see the text box content change.
        e.Handled = true;

        // Get content from the clipboard.
        var dataPackageView = Windows.ApplicationModel.DataTransfer.Clipboard.GetContent();
        if (dataPackageView.Contains(Windows.ApplicationModel.DataTransfer.StandardDataFormats.Text))
        {
            try
            {
                var text = await dataPackageView.GetTextAsync();

                // Remove line breaks from multi-line text and
                // replace with comma(,).
                string singleLineText = text.Replace("\r\n", ", ");

                // Replace any text currently in the text box.
                addressBox.Text = singleLineText;
            }
            catch (Exception)
            {
                // Ignore or handle exception as needed.
            }
        }
    }
}

Contoh ini memperlihatkan cara menangani peristiwa Tempel untuk membatasi jumlah karakter yang ditempelkan ke Dalam Kotak Teks. Jika panjang teks pada clipboard melebihi MaxLength dari TextBox, pesan akan ditampilkan kepada pengguna. Pengguna memiliki opsi untuk melanjutkan dengan teks terpotok, atau membatalkan operasi tempel.

<TextBox Paste="TextBox_Paste" MaxLength="10"/>
private async void TextBox_Paste(object sender, TextControlPasteEventArgs e)
{
    TextBox tb = sender as TextBox;
    if (tb != null)
    {
        // Mark the event as handled first. Otherwise, the
        // default paste action will happen, then the custom paste
        // action, and the user will see the text box content change.
        e.Handled = true;

        // Get content from the clipboard.
        var dataPackageView = Windows.ApplicationModel.DataTransfer.Clipboard.GetContent();
        if (dataPackageView.Contains(Windows.ApplicationModel.DataTransfer.StandardDataFormats.Text))
        {
            try
            {
                var text = await dataPackageView.GetTextAsync();
                if (text.Length > tb.MaxLength)
                {
                    // Pasted text is too long. Show a message to the user.
                    // Create the message dialog and set its content
                    var messageDialog = 
                        new Windows.UI.Popups.MessageDialog(
                            "Pasted text excedes maximum allowed (" 
                            + tb.MaxLength.ToString() + 
                            " characters). The text will be truncated.");

                    // Add commands to the message dialog.
                    messageDialog.Commands.Add(new UICommand("Continue", (command) =>
                    {
                        // Truncate the text to be pasted to the MaxLength of the text box.
                        string truncatedText = text.Substring(0, tb.MaxLength);
                        tb.Text = truncatedText;
                    }));
                    messageDialog.Commands.Add(new UICommand("Cancel", (command) =>
                    {
                        // Cancelled. Do nothing.
                    }));

                    // Set the command that will be invoked by default.
                    messageDialog.DefaultCommandIndex = 0;

                    // Set the command to be invoked when escape is pressed.
                    messageDialog.CancelCommandIndex = 1;

                    // Show the message dialog.
                    await messageDialog.ShowAsync();
                }
                else
                {
                    tb.Text = text;
                }                          
            }
            catch (Exception)
            {
                // Ignore or handle exception as needed.
            }
        }
    }
}

Keterangan

Peristiwa Tempel terjadi sebelum konten disisipkan ke dalam kontrol. Anda dapat menangani kejadian ini untuk memeriksa isi papan klip dan melakukan tindakan apa pun pada konten sebelum disisipkan. Jika Anda melakukan tindakan apa pun, atur properti Ditangani dari argumen peristiwa ke true; jika tidak, tindakan penempelan default dilakukan. Jika Anda menandai peristiwa sebagai ditangani, maka diasumsikan aplikasi telah menangani operasi tempel, dan tidak ada tindakan default yang dilakukan. Anda bertanggung jawab untuk menentukan titik penyisipan dan konten clipboard untuk disisipkan, dan menyisipkan konten.

Anda harus mengatur properti Ditangani ke true di handler Anda sebelum kode untuk melakukan tindakan tempel kustom. Jika tidak, tindakan tempel default dilakukan, lalu tindakan kustom dilakukan. Pengguna dapat melihat konten yang berubah di TextBox.

Berlaku untuk