TextBox.TextChanging Event
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Occurs synchronously when the text in the text box starts to change, but before it is rendered.
// 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"/>
Event Type
Examples
This example shows how to handle the TextChanging event to implement simple auto-complete for a 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);
}
}
Remarks
For event data, see TextBoxTextChangingEventArgs.
The TextChanging event occurs synchronously before the new text is rendered. In contrast, the TextChanged event is asynchronous and occurs after the new text is rendered.
When the TextChanging event occurs, the Text property already reflects the new value (but it's not rendered in the UI). You typically handle this event to update the Text value and selection before the text is rendered. This prevents the text flickering that can happen when text is rendered, updated, and re-rendered rapidly.
Note
This is a synchronous event that can occur at times when changes to the XAML visual tree are not allowed, such as during layout. Therefore, you should limit code within the TextChanging event handler primarily to inspecting and updating the Text property. Attempting to perform other actions, such as showing a popup or adding/removing elements from the visual tree, might cause potentially fatal errors that can lead to a crash. We recommend that you perform these other changes either in a TextChanged event handler, or run them as a separate asynchronous operation.
Applies to
See also
Geri Bildirim
https://aka.ms/ContentUserFeedback.
Çok yakında: 2024 boyunca, içerik için geri bildirim mekanizması olarak GitHub Sorunları’nı kullanımdan kaldıracak ve yeni bir geri bildirim sistemiyle değiştireceğiz. Daha fazla bilgi için bkz.Gönderin ve geri bildirimi görüntüleyin