225
Chương 6: Windows Form
Regex regex;
regex = new Regex(@"\S+@\S+\.\S+");
Control ctrl = (Control)sender;
if (regex.IsMatch(ctrl.Text)) {
errProvider.SetError(ctrl, "");
}
else {
errProvider.SetError(ctrl,
"This is not a valid e-mail address.");
}
}
private void cmdOK_Click(object sender, System.EventArgs e) {
string errorText = "";
bool invalidInput = false;
foreach (Control ctrl in this.Controls) {
if (errProvider.GetError(ctrl) != "")
{
errorText += " * " + errProvider.GetError(ctrl) + "\n";
invalidInput = true;
}
}
if (invalidInput) {
MessageBox.Show(
"The form contains the following unresolved errors:\n\n" +
errorText, "Invalid Input", MessageBoxButtons.OK,
MessageBoxIcon.Warning);
}
else {
this.Close();
}