Solved

textbox leave event creating an infinite loop

Posted on 2010-09-03
13
1,507 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
 
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

867 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now