I have a TextBox
and I want it to be numeric only. Thus, I googled and found this stackoverflow post which helped me already a lot. However, I want the, now numeric only, textbox to show a warning when I enter something underneath 20. At first, I tried the event OnTextInput
, and after no success I tried to handle it in the same event as the numeric stuff OnPreviewTextInput
. I used the same code for both.
if (!char.IsDigit(e.Text, e.Text.Length - 1))
{
var text = e.Text;
int num;
var success = int.TryParse(text, out num);
if (!success)
return;
if (num <= 20)
{
if (MessageBox.Show("Are you sure you want to go underneath 20?",
"... You sure?", MessageBoxButton.YesNo, MessageBoxImage.Exclamation) ==
MessageBoxResult.Yes)
e.Handled = true;
else
{
num++;
textBox.Text = num.ToString();
}
}
e.Handled = true;
}