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:
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.