You can either prevent any non-numeric input whatsoever, or just filter out digits in the text.
Preventing non-digit input
Use the BeforeTextChanging
event:
<TextBox BeforeTextChanging="TextBox_OnBeforeTextChanging" />
And now handle like this:
private void TextBox_OnBeforeTextChanging(TextBox sender,
TextBoxBeforeTextChangingEventArgs args)
{
args.Cancel = args.NewText.Any(c => !char.IsDigit(c));
}
This LINQ expression will return true
and hence Cancel
the text change in case it encounters any non-digit character in the input.
Filtering non-digit input
Use the TextChanging
event:
<TextBox TextChanging="TextBox_OnTextChanging" />
And handle this way:
private void TextBox_OnTextChanging(TextBox sender, TextBoxTextChangingEventArgs args)
{
//Save the position of the selection, to prevent the cursor to jump to the start
int pos = sender.SelectionStart;
sender.Text = new String(sender.Text.Where(char.IsDigit).ToArray());
sender.SelectionStart = pos;
}
This LINQ query will filter out non-digit characters and create a new string
only with the digits in the input.
It is preferable to use TextChanging
and BeforeTextChanging
, because TextChanged
occurs too late, so the user would be confused by seeing characters temporarily display on the screen and immediately disappearing.