Solved

textbox leave event creating an infinite loop

Posted on 2010-09-03
13
1,516 Views
Last Modified: 2013-12-17
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
Comment
Question by:felkamau
  • 5
  • 4
  • 3
  • +1
13 Comments
 
LVL 12

Expert Comment

by:w00te
ID: 33597212
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
 
LVL 8

Expert Comment

by:Gururaj Badam
ID: 33597340
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
 
LVL 5

Expert Comment

by:tvPrasad
ID: 33597378
Agree with w00te

 if (!txtBox.Focus())
                    txtBox.Focus();
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 8

Expert Comment

by:Gururaj Badam
ID: 33597472

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
 

Author Comment

by:felkamau
ID: 33597797
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
 
LVL 12

Expert Comment

by:w00te
ID: 33597916
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
 
LVL 8

Expert Comment

by:Gururaj Badam
ID: 33598030
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
 

Author Comment

by:felkamau
ID: 33598548
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
 
LVL 8

Accepted Solution

by:
Gururaj Badam earned 400 total points
ID: 33598597
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
 
LVL 12

Assisted Solution

by:w00te
w00te earned 100 total points
ID: 33598620
You didn't set induced to true so it did nothing.
0
 
LVL 12

Expert Comment

by:w00te
ID: 33598624
** Novice beat me to that... not so novice after all hehe :)
0
 
LVL 8

Expert Comment

by:Gururaj Badam
ID: 33598661
my mistake while copying the code missed that line ;)
0
 

Author Comment

by:felkamau
ID: 33598835
that did it!
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question