Solved

textbox leave event creating an infinite loop

Posted on 2010-09-03
13
1,480 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

707 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

17 Experts available now in Live!

Get 1:1 Help Now