I need to create a TextBox with only numbers but I couldn\'t do.
I have tried to put : InputScope = \"Numbers\" but this only work on Mobile.
Also I have tried on TextCha
You can do something else instead of TextBox.Text.Remove
function which is Substring
function and also you should fix it while they copy a series of characters which includes letters in the centre of the string:-
private void textBox1_TextChanged(object sender, EventArgs e)
{
var txt = textBox1.Text;
if (System.Text.RegularExpressions.Regex.IsMatch(txt, "[^0-9]"))
{
for (int i = 0; i < txt.Length; i++)
if (!char.IsDigit(txt[i]))
if (i != txt.Length - 1)
{
//If he pasted a letter inclusive string which includes letters in the middle
//Remove the middle character or remove all text and request number as input
//I did the second way
textBox1.Text = "";
break;
}
else
{
//If he just typed a letter in the end of the text
textBox1.Text = txt.Substring(0, txt.Length - 1);
break;
}
}
}
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)
{
sender.Text = new String(sender.Text.Where(char.IsDigit).ToArray());
}
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.
Here is an O(1) solution without string matching
bool textCanceled = false;
private void TextBox_KeyDown(object sender, KeyRoutedEventArgs e)
{
if (!Char.IsDigit((char)e.Key))
textCanceled = true;
else
textCanceled = false;
}
private void TextBox_BeforeTextChanging(TextBox sender, TextBoxBeforeTextChangingEventArgs args)
{
args.Cancel = textCanceled;
}
might have to handle backspace char as a special case