TextBox.TextChanging Événement

Définition

Se produit de manière synchrone lorsque le texte de la zone de texte commence à changer, mais avant qu’il ne soit 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 la 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 obtenir des données d’événement, consultez TextBoxTextChangingEventArgs.

L’événement TextChanging se produit de manière synchrone avant que le nouveau texte ne soit rendu. 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 Text et la sélection avant que le texte ne soit rendu. Cela empêche le scintillement du texte qui peut se produire lorsque le texte est rendu, mis à jour et rendu rapidement.

Notes

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, par exemple 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’effectuer d’autres actions, telles que l’affichage d’une fenêtre contextuelle ou l’ajout/la suppression d’éléments de l’arborescence visuelle, peut entraîner des erreurs potentiellement irrécupérables qui peuvent entraîner un incident. 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