《Microsoft Visual C# .NET 2003开发技巧大全》源代码
源代码在线查看: 8.12.txt
Listing 8.12 Displaying Errors with ErrorProvider
private void tbPhone_Validated(object sender, System.EventArgs e)
{
Regex phoneExp = new Regex( @”^\(\d{3}\)\s\d{3}-\d{4}$” );
if( tbPhone.Text == “” )
{
errorProvider1.SetError( tbPhone, “Please enter a phone number” );
}
else if( phoneExp.Match( tbPhone.Text ).Success == false )
{
errorProvider1.SetError( tbPhone, “Invalid Phone Number” );
}
else
{
errorProvider1.SetError( tbPhone, “” );
}
}
private void tbFirstName_Validated(object sender, System.EventArgs e)
{
// clear error if they filled in first name
if( tbFirstName.Text != “” )
errorProvider1.SetError( tbFirstName, “” );
}
private void tbLastName_Validated(object sender, System.EventArgs e)
{
// clear error since control is validated
errorProvider1.SetError( tbLastName, “” );
}