private void TextBox_TextComposition(object sender, TextCompositionEventArgs e) { // Handle the text composition event string composedText = e.Text; TextBox textBox = sender as TextBox; if (textBox != null) { int caretIndex = textBox.CaretIndex; textBox.Text = textBox.Text.Insert(caretIndex, composedText); textBox.CaretIndex += composedText.Length; e.Handled = true; } }
private void TextBox_TextInput(object sender, TextCompositionEventArgs e) { // Filter non-numeric characters if (!char.IsDigit(e.Text[0])) { e.Handled = true; } }In this example, we are filtering the input characters of a TextBox control. We are checking if the input character is a digit or not using the char.IsDigit() method. If it is not a digit, we are setting the Handled property of the TextCompositionEventArgs object to true to prevent the input from being inserted into the TextBox control. Package/Library: PresentationCore.dll