Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

textbox leave event creating an infinite loop

Posted on 2010-09-03
13
Medium Priority
?
1,663 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
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!

 
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 1600 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 400 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

618 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