Click here to Skip to main content
15,901,373 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
private void txtAccCentre_Leave(object sender, EventArgs e)
       {

           if (txtAccCentre.Text.Trim() != "")
           {

               if (txtAccCentre.Text.Length >= 3)
               {
                   txtCurrency.Text = string.Empty;
                   cbCurrency.DataSource = null;
                   txtAccCentre.LostFocus += new EventHandler(txtAccCentre_LostFocus);
               }
               else
               {
                   string result = CustomMessageBox.ShowBox("Filter text length should be greater than or equal to 3", "Error", "");
                   if (txtAccCentre.Text.Length == 0)
                   {
                       txtAccCentre.Text = "Code/Desc";
                       txtAccCentre.ForeColor = SystemColors.GrayText;
                   }

               }
           }
       }



My textBox leave triggers twice? why it is ... I tried by removing
C#
txtAccCentre.LostFocus += new EventHandler(txtAccCentre_LostFocus);

this line.but it exists
Posted
Comments
Sinisa Hajnal 5-Jan-16 4:28am    
Lost focus removal should do it. Leave is new(er) event that replaces lost focus.

Check that you're not setting the focus back in some other event.
Sathish km 5-Jan-16 4:30am    
no i have txtAccCentre_LostFocus event..
dan!sh 5-Jan-16 4:47am    
I guess ShowBox method is displaying a MessageBox using Showdialog method. Can you remove call to ShowBox and check? Also, check the call stack on the second event handler call to see execution flow details for hints.

1 solution

Because when it triggers you are adding an event handler to the LostFocus chain.
So the next time the user leaves, the LostFocus event handler method is called twice, then three times, then four...
Set the handler in the designer, and check inside that handler if it needs to process rather than adding it dynamically.

If that doesn't cure it, check that what you think is happening is exactly what is happening - add Debug.WriteLine statements to each of your handlers so that you can identify exactly which handlers are being called, and in what order.

We can't do much to help you directly here - that little fragment doesn't tell us enough about how the rest of your code works!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900