The constructor and form load event handler are both in their default conditions. Following this section of code, the next thing up to look at is the validation handler for the non-empty string text box control:
/// <summary>
/// Validate to make sure that the textbox contains
/// a string with at least one character
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void txtEmptyString_Validated(object sender, EventArgs e)
{
bool bTest = txtEmptyStringIsValid();
if (bTest == true)
{
this.errorProvider1.SetError(txtEmptyString, "This field must contain text");
}
else
{
this.errorProvider1.SetError(txtEmptyString, "");
}
}
There are couple of interesting things to note in this handler; first off note that a boolean is set to capture the results of a method called "txtEmptyStringIsValid"; this is the method that actually tests the contents of the text box to make sure that it is not empty. As a result of the test, the error provider control is configured; if the string is empty, the error provider is associated with the text box control and the error provider's control is set to display "This field must contain text". If the string is not empty, the error provider is set to display nothing; this disables the error provider as applied to that control. The result is that, whenever the text box control is validated, the test will execute and, if failed, the error provider icon will be displayed. If the user hovers the cursor over the error provider's icon, the tool tip text will be displayed. If the string is not empty, the error provider icon will not appear and there will be no tooltip.
The method used to validate the control is as follows:
/// <summary>
/// Test for empty string in the text box and
/// return the results
/// </summary>
/// <returns>boolean</returns>
private bool txtEmptyStringIsValid()
{
if (txtEmptyString.Text == string.Empty)
{
return true;
}
else
{
return false;
}
}
This simple method is configured to return a boolean value; if the text box control contains text, the function will return false and no error provider indication will be shown on the form. If the string is empty, the method will return true and the error provider will be used to mark the empty string text box. This is essentially how all of the validation event handlers perform in this demo application; that is, the validation event handler calls the method used to test the associated control and then, as a result of that test, either the error indication is shown or not shown.
The next item up is the validation event handler for the regular expression test text box:
/// <summary>
/// Test the contents of this text box against a regular expression
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void txtRegExString_Validated(object sender, EventArgs e)
{
bool bTest = txtRegExStringIsValid(txtRegExString.Text.ToString());
if (bTest == false)
{
this.errorProvider1.SetError(txtRegExString, "This field must
contain a phone number (222-333-4444)");
}
else
{
this.errorProvider1.SetError(txtRegExString, "");
}
}
This method creates an instance of and populates a regular expression object. The regular expression shown is used to validate a US telephone number. The text to validate property is the contents of the associated text box control. The regular expression object's "IsMatch" method is used to validate the text and, as a result of the test, the method returns a boolean value set to either true or false.
The next validation event handler and custom validation method are used to validate that the text box control contains only numeric values. The custom validation method also makes sure that the text box control is not empty. To test the text box, the contents of the text box are dumped into a character array and each character in that array is evaluated to make sure that is does not contain non-numeric values.
/// <summary>
/// Validate that the textbox contains only numbers
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void txtNumericString_Validated(object sender, EventArgs e)
{
bool bTest = txtNumericStringIsValid();
if (bTest == true)
{
this.errorProvider1.SetError(txtNumericString, "This field
must contain only numbers");
}
else
{
this.errorProvider1.SetError(txtNumericString, "");
}
}
/// <summary>
/// Test for non-numeric values in the text box and
/// also make sure the textbox is not empty
/// </summary>
/// <returns>boolean</returns>
private bool txtNumericStringIsValid()
{
if (txtNumericString.Text == string.Empty)
{
return true;
}
char[] testArr = txtNumericString.Text.ToCharArray();
bool testBool = false;
for (int i = 0; i < testArr.Length; i++)
{
if (!char.IsNumber(testArr[i]))
{
testBool = true;
}
}
return testBool;
}
The next pair is used to validate on alphas only, it works essentially the same way as the previous example:
/// <summary>
/// Test to validate that this textbox only contains
/// alphas
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void txtAlphaString_Validated(object sender, EventArgs e)
{
bool bTest = txtAlphaStringIsValid();
if (bTest == true)
{
this.errorProvider1.SetError(txtAlphaString, "This field must
contain only alphas");
}
else
{
this.errorProvider1.SetError(txtAlphaString, "");
}
}
/// <summary>
/// Test for non-alpha values in the text box and
/// also make sure that the textbox is not empty
/// </summary>
/// <returns>boolean</returns>
private bool txtAlphaStringIsValid()
{
// first make sure the textbox contains something
if (txtAlphaString.Text == string.Empty)
{
return true;
}
// test each character in the textbox
char[] testArr = txtAlphaString.Text.ToCharArray();
bool testBool = false;
for (int i = 0; i < testArr.Length; i++)
{
if (!char.IsLetter(testArr[i]))
{
testBool = true;
}
}
return testBool;
}
Next up is the range validation example; this example is used to test to see if the value entered is a numeric value between 50 and 100. One thing interesting to note in this example is that two error providers are used; one shows up on valid entries and one shows up on invalid entries. This was done to demonstrate using the control to show status other than failure (in the form of a confirmation of a good entry).
///
<summary>
/// Validate that the textbox contains a value
/// that is between 50 and 100
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void txtRangeValidation_Validated(object sender, EventArgs e)
{
bool bTest = txtRangeValidationIsValid();
if (bTest == false)
{
this.errorProvider1.SetError(txtRangeValidation, "This field
must contain a number between 50 and 100");
this.errorProvider2.SetError(txtRangeValidation, "");
}
else
{
this.errorProvider1.SetError(txtRangeValidation, "");
this.errorProvider2.SetError(txtRangeValidation, "The value
is between 50 and 100");
}
}
/// <summary>
/// Test for numeric values between 50 and 100 in the text box and
/// return the results
/// </summary>
/// <returns>boolean</returns>
private bool txtRangeValidationIsValid()
{
int tmpVal = 0;
try
{
tmpVal = Convert.ToInt32(txtRangeValidation.Text);
}
catch { }
bool testBool = false;
if (tmpVal < 100 && tmpVal > 50)
{
testBool = true;
}
return testBool;
}
The next two validation examples each test the contents of the associated text boxes for different types of contents (alphanumeric, and special characters):
/// <summary>
/// Validate that the text box contains only numbers
/// or letters
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void txtAlphaNumericString_Validated(object sender, EventArgs e)
{
bool bTest = txtAlphaNumericStringIsValid();
if (bTest == true)
{
this.errorProvider1.SetError(txtAlphaNumericString, "This field must contain only alphanumerics");
}
else
{
this.errorProvider1.SetError(txtAlphaNumericString, "");
}
}
/// <summary>
/// Test for non-alpha values in the text box and
/// return the results
/// </summary>
/// <returns>boolean</returns>
private bool txtAlphaNumericStringIsValid()
{
// Make sure the string is not empty first
if (txtAlphaNumericString.Text == string.Empty)
{
return true;
}
// check for alphanumeric values
char[] testArr = txtAlphaNumericString.Text.ToCharArray();
bool testBool = false;
for (int i = 0; i < testArr.Length; i++)
{
if (!char.IsLetter(testArr[i]) && !char.IsNumber(testArr[i]))
{
testBool = true;
}
}
return testBool;
}
/// <summary>
/// Validate that this text box contains only special characters
/// (non-alphanumerics)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void txtSpecialChars_Validated(object sender, EventArgs e)
{
bool bTest = txtSpecialCharsIsValid();
if (bTest == false)
{
this.errorProvider1.SetError(txtSpecialChars, "This field must contain only special characters");
}
else
{
this.errorProvider1.SetError(txtSpecialChars, "");
}
}
/// <summary>
/// Test for special character values in the textbox
/// </summary>
/// <returns>boolean</returns>
private bool txtSpecialCharsIsValid()
{
// Make sure the string is not empty first
if (txtSpecialChars.Text == string.Empty)
{
return false;
}
char[] testArr = txtSpecialChars.Text.ToCharArray();
bool testBool = false;
for (int i = 0; i < testArr.Length; i++)
{
if (!char.IsSymbol(testArr[i]))
{
testBool = true;
}
if(char.IsLetterOrDigit(testArr[i]))
{
testBool = false;
}
}
return testBool;
}
The next example is used to validate that a text box contains a minimum number of characters (3):
/// <summary>
/// Validate that the text box contains a minimum
/// number of characters
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void txtMinLengthTest_Validated(object sender, EventArgs e)
{
bool bTest = txtMinLengthTestIsValid();
if (bTest == true)
{
this.errorProvider1.SetError(txtMinLengthTest,
"This field must contain at least 3 characters");
}
else
{
this.errorProvider1.SetError(txtMinLengthTest, "");
}
}
/// <summary>
/// Test to see that the textbox contains a minimum number
/// of characters
/// </summary>
/// <returns>boolean</returns>
private bool txtMinLengthTestIsValid()
{
char[] testArr = txtMinLengthTest.Text.ToCharArray();
bool testBool = false;
if (testArr.Length < 3)
{
testBool = true;
}
return testBool;
}
The last bit of code worth mentioning is used to validate all of the controls at the same time; this might be a useful thing to do to make sure all of the controls contain valid content prior to saving or posting data:
/// <summary>
/// Validate all of the controls at the same time from
/// a button click event handler
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnValidateAll_Click(object sender, EventArgs e)
{
txtEmptyString_Validated(this, e);
txtAlphaNumericString_Validated(this, e);
txtAlphaString_Validated(this, e);
txtMinLengthTest_Validated(this, e);
txtNumericString_Validated(this, e);
txtRangeValidation_Validated(this, e);
txtRegExString_Validated(this, e);
txtSpecialChars_Validated(this, e);
}
This method merely calls for validation of each control; doing so will validate each control and will result in the display of error provider on each detected failure of the specific validations tests.