Langage

TextBox.TextChanging Événement

Définition

Se produit de façon synchrone lorsque le texte de la zone de texte commence à changer, mais avant son rendu.

// 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"/>

Type d’événement

Exemples

Cet exemple montre comment gérer l’événement TextChanging pour implémenter une saisie semi-automatique simple pour une zone de texte.

<!-- 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);
    }
}

Remarques

Pour les données d’événement, consultez TextBoxTextChangingEventArgs.

L’événement TextChanging se produit de façon synchrone avant le rendu du nouveau texte. En revanche, l’événement TextChanged est asynchrone et se produit après le rendu du nouveau texte.

Lorsque l’événement TextChanging se produit, la propriété Text reflète déjà la nouvelle valeur (mais elle n’est pas rendue dans l’interface utilisateur). Vous gérez généralement cet événement pour mettre à jour la valeur de texte et la sélection avant le rendu du texte. Cela empêche le scintillement du texte qui peut se produire lorsque le texte est rendu, mis à jour et réexécrit rapidement.

Note

Il s’agit d’un événement synchrone qui peut se produire à des moments où les modifications apportées à l’arborescence visuelle XAML ne sont pas autorisées, comme pendant la disposition. Par conséquent, vous devez limiter le code dans le gestionnaire d’événements TextChanging principalement à l’inspection et à la mise à jour de la propriété Text . La tentative d’exécution d’autres actions, telles que l’affichage d’une fenêtre contextuelle ou l’ajout/suppression d’éléments de l’arborescence visuelle, peut entraîner des erreurs potentiellement irrécupérables pouvant entraîner un blocage. Nous vous recommandons d’effectuer ces autres modifications dans un gestionnaire d’événements TextChanged ou de les exécuter en tant qu’opération asynchrone distincte.

S’applique à

Voir aussi