我可以为(例如)带有一个错误提供程序的6个文本框处理多个验证吗?
答案 0 :(得分:1)
示例为每个文本框提供一个错误(如果为空),并指定错误所针对的文本框。
*使用开关编辑
private void Form1_Load(object sender, EventArgs e)
{
foreach (TextBox textBox in Controls) { textBox.Validating += textBox1_Validating; }
}
private void textBox1_Validating(object sender, CancelEventArgs e)
{
ErrorProvider errProv = new ErrorProvider();
TextBox txtSender = (TextBox)sender;
string content = txtSender.Text;
string error = "";
switch (txtSender.Name)
{
case "textBox1":
error = "error textbox1";
break;
case "textBox2":
error = "error textbox2";
break;
case "textBox3":
error = "error textbox3";
break;
case "textBox4":
error = "error textbox4";
break;
case "textBox5":
error = "error textbox5";
break;
case "textBox6":
error = "error textbox6";
break;
case "textBox7":
error = "error textbox7";
break;
case "textBox8":
error = "error textbox8";
break;
}
if (content == "") { errProv.SetError(txtSender, error); }
}