I want validation module to work when the user leaves the textbox or the textbox is out of focus. i.e. when the user input in one textbox and then focus on other textbox for input instantly error message should popup if the previous input is not valid.
Suppose ::
Username :: //txtUsername
Phone :: //txtPhone
here error message should popup instantly for txtName if it is invalid when txtName is out of focus.
I've develop a module for validation but it works when the form is complete,i.e. only at the save time.
#region "Input Validations"
private bool IsClientValid()
{
if (txtUsername.Text == string.Empty || txtUsername.Text == null)
{
MessageBox.Show("User Name is required", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
if (txtUsername.Text.Length > 200)
{
MessageBox.Show("User Name cannot be more than 200 characters", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
if (txtPhone.Text.Length > 20)
{
MessageBox.Show("Phone Number cannot be more than 20 characters", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
return true;
}
#endregion

Manoj BhoirRank #599 · 4K reputationPosted May 21, 2015, 12:26 AM EDT
private void textBox1_LostFocus(object sender, System.EventArgs e) { if (IsClientValid() == false){ //Show here Error Message. } }Manoj BhoirRank #599 · 4K reputationPosted May 20, 2015, 9:01 PM EDT
- Enter
- GotFocus
- Leave
- Validating
- Validated
- LostFocus
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
If the CausesValidation property is set to false, the Validating and Validated events are suppressed.If the Cancel property of the CancelEventArgs is set to true in the Validating event delegate, all events that would usually occur after the Validating event are suppressed.
private void textBox1_Validating(object sender, System.ComponentModel.CancelEventArgs e) { // If nothing is entered, // an ArgumentException is caught; if an invalid directory is entered, // a DirectoryNotFoundException is caught. An appropriate error message // is displayed in either case. try { System.IO.DirectoryInfo directory = new System.IO.DirectoryInfo(textBox1.Text); directory.GetFiles(); errorProvider1.SetError(textBox1, ""); } catch(System.ArgumentException ex1) { errorProvider1.SetError(textBox1, "Please enter a directory"); } catch(System.IO.DirectoryNotFoundException ex2) { errorProvider1.SetError(textBox1, "The directory does not exist." + "Try again with a different directory."); } } // This method handles the LostFocus event for textBox1 by setting the // dialog's InitialDirectory property to the text in textBox1. private void textBox1_LostFocus(object sender, System.EventArgs e) { openFileDialog1.InitialDirectory = textBox1.Text; } // This method demonstrates using the ErrorProvider.GetError method // to check for an error before opening the dialog box. private void button1_Click(System.Object sender, System.EventArgs e) { //If there is no error, then open the dialog box. if (errorProvider1.GetError(textBox1)=="") { DialogResult dialogResult = openFileDialog1.ShowDialog(); } }Nanhe SiddiqueRank #1,103 · 1.7K reputationPosted May 20, 2015, 9:10 PM EDT
hadoop hadoop142 reputationPosted May 20, 2015, 9:47 PM EDT