• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1691
  • Last Modified:

textbox leave event creating an infinite loop

I am using c# 1.1.
I have the following code validation a date field...

private void txtBox_Leave(object sender, System.EventArgs e)
   {
     try
         {
             //check for textbox value;
          }
     catch
          {
             MessageBox.Show("Date must be a valid Date Time Format: XX/XX/XXXX");
             txtBox.Focus();
          }
     }

This is creating an infinite loop when the user deletes the date and tries to navigate to the next screen


0
felkamau
Asked:
felkamau
  • 5
  • 4
  • 3
  • +1
2 Solutions
 
w00teCommented:
It's creating an infinite loop because your test box leave calls text box focus, but test box focus will cause the text box to leave prior to setting the focus.  Ad an if statement to check and see if the text box is currently focused, and if its dont call Foucs().
-w00te
0
 
Gururaj BadamCommented:
In the try block if you're parsing the Date you better you DateTime.TryParse so it won't throw exception. And based on your parse success you can set the focus and exist.
0
 
tvPrasadCommented:
Agree with w00te

 if (!txtBox.Focus())
                    txtBox.Focus();
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Gururaj BadamCommented:

private void txtBox_Leave(object sender, System.EventArgs e)
       {
         try
             {
                 //check for textbox value;
                 DateTime dt;

                 if (!DateTime.TryParse(txtBox.Text, formatProvider, System.Globalization.DateTimeStyles.None, out dt) && !txtBox.Focus())
                     txtBox.Focus();
              }
         catch
              {
                 MessageBox.Show("Date must be a valid Date Time Format: XX/XX/XXXX");

              }
        }

Open in new window

0
 
felkamauAuthor Commented:
Still creating infinite loop

I have tried the following:

private void txtBox_Leave(object sender, System.EventArgs e)
   {
     try
         {
             //check for textbox value;
          }
     catch
          {
             MessageBox.Show("Date must be a valid Date Time Format: XX/XX/XXXX");
             if (!txtBox.Focus())
                {
                  txtBox.Focus();
                 }
          }
     }
0
 
w00teCommented:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.leave.aspx

When you change the focus by using the mouse or by calling the Focus method, focus events occur in the following order:
-Enter
-GotFocus
-LostFocus
-Leave
-Validating
-Validated
You're calling the Focus method, which causes LostFocus before leave, which means you're going to always call the focus thing again even with your guard (because your event is leave!).
Try using a different method to reselect the control, maybe select?  You shouldnt have that focus call in that event, that's all there is to it.
http://msdn.microsoft.com/en-us/library/7wt11hea.aspx
--> select
-w00te
 
0
 
Gururaj BadamCommented:
private bool m_induced = false;

private void txtBox_Leave(object sender, System.EventArgs e)
   {
     try
         {
             //check for textbox value;
          }
     catch
          {
             MessageBox.Show("Date must be a valid Date Time Format: XX/XX/XXXX");
             if (!induced && !txtBox.Focused)
                {
                  txtBox.Focus();
                 }
          }
     }
0
 
felkamauAuthor Commented:
I think the try catch is what is causing the loop. It will keep trying to validate the field indefinately. Is there a way to interrupt the try catch....will finally work
0
 
Gururaj BadamCommented:
the induced declaration is to break loop

private bool m_induced = false;


private void txtBox_Leave(object sender, System.EventArgs e)
   {
     try
         {
             //check for textbox value;
          }
     catch
          {
             MessageBox.Show("Date must be a valid Date Time Format: XX/XX/XXXX");
             if (!induced && !txtBox.Focused)
                {
induced = true;
                  txtBox.Focus();
                 }
          }
     }
0
 
w00teCommented:
You didn't set induced to true so it did nothing.
0
 
w00teCommented:
** Novice beat me to that... not so novice after all hehe :)
0
 
Gururaj BadamCommented:
my mistake while copying the code missed that line ;)
0
 
felkamauAuthor Commented:
that did it!
0

Featured Post

Free recovery tool for Microsoft Active Directory

Veeam Explorer for Microsoft Active Directory provides fast and reliable object-level recovery for Active Directory from a single-pass, agentless backup or storage snapshot — without the need to restore an entire virtual machine or use third-party tools.

  • 5
  • 4
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now