TextBox.TextChanging Evento
Definizione
Importante
Alcune informazioni sono relative alla release non definitiva del prodotto, che potrebbe subire modifiche significative prima della release definitiva. Microsoft non riconosce alcuna garanzia, espressa o implicita, in merito alle informazioni qui fornite.
Si verifica in modo sincrono quando il testo nella casella di testo inizia a cambiare, ma prima di eseguirne il rendering.
// 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"/>
Tipo evento
Esempio
In questo esempio viene illustrato come gestire l'evento TextChanging per implementare un semplice completamento automatico per un controllo 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);
}
}
Commenti
Per i dati dell'evento, vedere TextBoxTextChangingEventArgs.
L'evento TextChanging si verifica in modo sincrono prima del rendering del nuovo testo. Al contrario, l'evento TextChanged è asincrono e si verifica dopo il rendering del nuovo testo.
Quando si verifica l'evento TextChanging, la proprietà Text riflette già il nuovo valore (ma non viene eseguito il rendering nell'interfaccia utente). Questo evento viene in genere gestito per aggiornare il valore text e la selezione prima del rendering del testo. In questo modo viene impedito lo sfarfallio del testo che può verificarsi quando viene eseguito il rendering, l'aggiornamento e il rendering rapido del testo.
Note
Si tratta di un evento sincrono che può verificarsi a volte quando le modifiche alla struttura ad albero visuale XAML non sono consentite, ad esempio durante il layout. Pertanto, è consigliabile limitare il codice all'interno del gestore eventi TextChanging principalmente per controllare e aggiornare la proprietà Text . Il tentativo di eseguire altre azioni, ad esempio la visualizzazione di un popup o l'aggiunta/rimozione di elementi dalla struttura ad albero visuale, potrebbe causare errori potenzialmente irreversibili che possono causare un arresto anomalo. È consigliabile eseguire queste altre modifiche in un gestore eventi TextChanged oppure eseguirle come operazione asincrona separata.