TextBox.TextChanging Kejadian

Definisi

Terjadi secara sinkron ketika teks dalam kotak teks mulai berubah, tetapi sebelum dirender.

// Register
event_token TextChanging(TypedEventHandler<TextBox, TextBoxTextChangingEventArgs const&> const& handler) const;

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

// Revoke with event_revoker
TextBox::TextChanging_revoker TextChanging(auto_revoke_t, TypedEventHandler<TextBox, TextBoxTextChangingEventArgs const&> const& handler) const;
public event TypedEventHandler<TextBox,TextBoxTextChangingEventArgs> TextChanging;
function onTextChanging(eventArgs) { /* Your code */ }
textBox.addEventListener("textchanging", onTextChanging);
textBox.removeEventListener("textchanging", onTextChanging);
- or -
textBox.ontextchanging = onTextChanging;
Public Custom Event TextChanging As TypedEventHandler(Of TextBox, TextBoxTextChangingEventArgs) 
<TextBox TextChanging="eventhandler"/>

Jenis Acara

Contoh

Contoh ini menunjukkan cara menangani peristiwa TextChanging untuk menerapkan penyelesaian otomatis sederhana untuk TextBox.

<!-- Text box in MainPage.xaml -->
<TextBox x:Name="textBox" TextChanging="textBox_TextChanging"
         Width="200" Height="32"/>
public sealed partial class MainPage : Page
{
    // Boolean to keep track of whether or not you should ignore the next TextChanged event.  
    // This is needed to support the correct behavior when backspace is tapped.
    public bool m_ignoreNextTextChanged = false;

    // Sample list of strings to use in the autocomplete.
    public string[] m_options = { "microsoft.com", "dev.windows.com", "msn.com", "office.com", "msdn.microsoft.com" };

    public MainPage()
    {
        this.InitializeComponent();
    }

    private void textBox_TextChanging(TextBox sender, TextBoxTextChangingEventArgs args)
    {
        // Needed for the backspace scenario.
        if (m_ignoreNextTextChanged)
        {
            m_ignoreNextTextChanged = false;
            return;
        }
        // All other scenarios other than the backspace scenario.
        // Do the auto complete.
        else
        {
            string s = textBox.Text;
            if (s.Length > 0)
            {
                for (int i = 0; i < m_options.Length; i++)
                {
                    if (m_options[i].IndexOf(s) >= 0)
                    {
                        if (s == m_options[i])
                            break;

                        textBox.Text = m_options[i];
                        textBox.Select(s.Length, m_options[i].Length - s.Length);
                        break;
                    }
                }
            }
        }
    }

    protected override void OnKeyDown(KeyRoutedEventArgs e)
    {
        if (e.Key == Windows.System.VirtualKey.Back
            || e.Key == Windows.System.VirtualKey.Delete)
        {
            m_ignoreNextTextChanged = true;
        }
        base.OnKeyDown(e);
    }
}

Keterangan

Untuk data peristiwa, lihat TextBoxTextChangingEventArgs.

Peristiwa TextChanging terjadi secara sinkron sebelum teks baru dirender. Sebaliknya, peristiwa TextChanged tidak sinkron dan terjadi setelah teks baru dirender.

Saat peristiwa TextChanging terjadi, properti Teks sudah mencerminkan nilai baru (tetapi tidak dirender di UI). Anda biasanya menangani peristiwa ini untuk memperbarui nilai Teks dan pilihan sebelum teks dirender. Ini mencegah kedipan teks yang dapat terjadi ketika teks dirender, diperbarui, dan dirender ulang dengan cepat.

Catatan

Ini adalah peristiwa sinkron yang dapat terjadi pada saat perubahan pada pohon visual XAML tidak diizinkan, seperti selama tata letak. Oleh karena itu, Anda harus membatasi kode dalam penanganan aktivitas TextChanging terutama untuk memeriksa dan memperbarui properti Teks . Mencoba melakukan tindakan lain, seperti menampilkan popup atau menambahkan/menghapus elemen dari pohon visual, dapat menyebabkan kesalahan yang berpotensi fatal yang dapat menyebabkan crash. Kami menyarankan agar Anda melakukan perubahan lain ini baik dalam penanganan aktivitas TextChanged , atau menjalankannya sebagai operasi asinkron terpisah.

Berlaku untuk

Lihat juga