0

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;
}
Community
  • 1
  • 1
John
  • 197
  • 1
  • 2
  • 22
  • you say the number must not go below 20, but then you have a comparison: num <= 20 This includes 20. If it is okay for your textbox to hold "20", then you will want to remove the = sign from that comparison. (This comment is because your title and your code doesn't completely match up) – Tyler Aug 25 '16 at 10:50
  • Yes I want 20 to be still ok – John Aug 25 '16 at 10:53
  • i believe this post will also help you: http://stackoverflow.com/questions/2587602/textbox-value-changed It talks about a TextChanged event for TextBox, so you probably can do something like - On TextChanged get TextBox value, make sure it is a number, if so. Make sure it is greater than or equal to 20 (or if it is less than 20 do X) – Tyler Aug 25 '16 at 10:54
  • 4
    Why don't you use NumericTextBox? – Mr.B Aug 25 '16 at 10:55

2 Answers2

1

Use TextChanged Handler to catch when it changes, then make the filter.

Following your implementation can looks like this:

private void yourTextBox_TextChanged(object sender, EventArgs e)
{
    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;
        }
    }
Raskayu
  • 735
  • 7
  • 20
  • Something I totally missed was the fact that whenever somebody now writes 75%, the 7 is handled, too, which thus means I can't enter ANY number without notice... Any idea? :-O – John Aug 25 '16 at 10:56
  • @John what about checking first if `yourTextBox.Text.Length>1` – Pikoh Aug 25 '16 at 10:58
  • @Pikoh then I can't handle if input is 3, which is also forbidden – John Aug 25 '16 at 11:07
  • Yes, you can. But anyway,this seems a poor design to me,and difficult to implement. I suggest you use any number specific control instead of a textbox,and check the values when validating the control or the form – Pikoh Aug 25 '16 at 11:11
  • I agree with @Pikoh. Even if you wanna do it like this, treat it as a number not a char. – Raskayu Aug 25 '16 at 11:15
1

Why don't you handle the KeyDown event instead?

private void txtNumeric_KeyDown(object sender, KeyEventArgs e)  
 {  
    e.Handled = !char.IsDigit(e.KeyChar); 
    if (Int32.Parse(sender.Text) <= 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();
            }

 } 
DNKROZ
  • 2,634
  • 4
  • 25
  • 43