Click here to Skip to main content
15,888,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am designing a data entry form.
The UI consists of:
1. textbox for user name
2. textbox for user code
3. listview showing a list of names of existing users
4. button to add new user

Now, handling validating event for the 2 textboxes, I have written this code:
C#
void tb_Name_Validating(object sender, CancelEventArgs e)
{
  try
  {
    if(tb_Name.TextLength == 0)
      {
        tb_Code.Enabled = false;
        tb_Name.ForeColor = Color.White;
        tb_Name.BackColor = Color.PaleVioletRed;
        tb_Name.Focus();
      }
  }
  catch (Exception ex)
  {
    MessageBox.Show(ex.ToString());
  }
}


I am not allowing user to move to next control unless user has provided a value for the user name.

Now, problem comes when the input focus is still at tb_Name and user clicks on an item in the listview control.
As soon as user clicks on the listview item, validating event for the textbox tb_Name is fired. And the focus is again remained on the textbox itself with an error (changing backcolor).

I want to bypass validating event. But, I dont know how to tell c# that user has moved to some control other than tb_Code. The validating event must occured only when user tries to move to control tb_Code.

Any help will be highly appreciated.
Thank you!

What I have tried:

I tried going through the order of events for Enter,Leave, GotFocus, LostFocus, validating and validated.
Also, I tried many alternatives but none worked perfectly.
Posted
Updated 4-Feb-17 5:58am
v2

1 solution

If you want to validate only when the user enters tb_Code, then use the tb_Code.Enter event, and move the validation code there.
 
Share this answer
 
Comments
Member 11040029 5-Feb-17 1:00am    
I dont need it when user enters textbox. Griff pls read the query again carefully... if u need more details I can tell
OriginalGriff 5-Feb-17 2:30am    
And I quote: "The validating event must occured only when user tries to move to control tb_Code."
Say what you mean then. We cannot read your mind! :laugh:
Member 11040029 5-Feb-17 7:15am    
user will only move to tb_Code when he leaves tb_Name... so *Leaving* is the first
OriginalGriff 5-Feb-17 9:13am    
Again, let me quote:
"when the input focus is still at tb_Name and user clicks on an item in the listview control.
As soon as user clicks on the listview item, validating event for the textbox tb_Name is fired"
When your user clicks on the listview, the textbox loses the focus, and the validating event is fired - it does not matter where the focus is going, and it shouldn't matter - because once he has left the textbox, he can go anywhere he wants - including to your tb_Code. If the validation has not occurred, then he can enter bad values, click on the ListView, then click on tb_Code without a validation occurring. I would suggest that the action you have at the moment of marking it as an error and not letting him leave the TextBox is correct.

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