5

I am stuck into a problem and need inputs. Here is the description -

I have a txtPenaltyDays in windows form C#

private void txtPenaltyDays_TextChanged(object sender, EventArgs e)
{
  if(Convert.ToInt16(txtPenaltyDays.Text) > 5)
  {
    MessageBox.Show("The maximum amount in text box cant be more than 5"); 
    txtPenaltyDays.Text = 0;// Re- triggers the TextChanged 
  }
}

But I am running into problem because this fires 2 times. Because of setting the text value to 0. My requirement is that it should fire only one time and set the value to 0.

Any suggestion is deeply appreciated.

3
  • a more serious problem. What happen if your user types a letter ? Commented Oct 9, 2014 at 17:31
  • if(txtPenaltyDays.Text=="0") return; Commented Oct 9, 2014 at 17:32
  • Yes steve its true . this is just a part of code i tried to put . But in actual code i have key-press event handling all the special characters Commented Oct 9, 2014 at 17:33

5 Answers 5

8

Just disable the event handler when you discover the invalid value, inform the user and then reenable the event handler

 private void txtPenaltyDays_TextChanged(object sender, EventArgs e)
 {
   short num;
   if(Int16.TryParse(txtPenaltyDays.Text, out num))
   {
       if(num > 5)
       {
           txtPenaltyDays.TextChanged -= txtPenaltyDays_TextChanged;
           MessageBox.Show("The maximum amount in text box cant be more than 5"); 
           txtPenaltyDays.Text = "0";//
           txtPenaltyDays.TextChanged += txtPenaltyDays_TextChanged;
       }
   }
   else
   {
      txtPenaltyDays.TextChanged -= txtPenaltyDays_TextChanged;
      MessageBox.Show("Typed an invalid character- Only numbers allowed"); 
      txtPenaltyDays.Text = "0";
      txtPenaltyDays.TextChanged += txtPenaltyDays_TextChanged;
   }
 }

Notice also that I have removed the Convert.ToInt16 because it fails if your user types a letter instead of a number and used Int16.TryParse

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Steve...that works! Thanks guys for your inputs however i understand that private variable option was not a good one because this will prevent re-trigger on application level.
Did you consider to use the NumericUpDown control? It has Minimum and Maximum properties that control the allowable values. And it removes the key press handling code. For simple scenarios it is a valuable option
4

You can use a private form field to keep the event from firing the 2nd time:

private bool _IgnoreEvent = false;

private void txtPenaltyDays_TextChanged(object sender, EventArgs e)
 {
   if (_IgnoreEvent) { return;}
   if(Convert.ToInt16(txtPenaltyDays.Text)>5)
    MessageBox.Show("The maximum amount in text box cant be more than 5"); 
    _IgnoreEvent = true;
    txtPenaltyDays.Text = 0;// Re- triggers the TextChanged, but will be ignored 
    _IgnoreEvent = false;
 }

A better question would be, "should I do this in TextChanged, or would it be better to do it in Validating?"

1 Comment

This is the best method. Adding and removing events for something so small is not a good pattern
3

Try following code

private void txtPenaltyDays_TextChanged(object sender, EventArgs e)
{
   if(Convert.ToInt16(txtPenaltyDays.Text)>5)
   {
      MessageBox.Show("The maximum amount in text box cant be more than 5"); 
      txtPenaltyDays.TextChanged -= txtPenaltyDays_TextChanged; 
      txtPenaltyDays.Text = 0;// Re- triggers the TextChanged 
      txtPenaltyDays.TextChanged += txtPenaltyDays_TextChanged;
   }
}

Comments

1

You could use the event Leave or LostFocus instead.

Comments

1

You can check if textbox is not focused then fire the event:

if (!textbox1.Focused) return;

or bind and unbind the event:

textbox1.TextChanged -= textbox1_TextChanged; textbox.Text = "some text"; textbox1.TextChanged += textbox1_TextChanged;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.