C# 使用errorprovider处理多个错误

C# 使用errorprovider处理多个错误,c#,error-handling,C#,Error Handling,我是否可以使用一个错误提供程序处理(例如)6个文本框的多个验证?示例为每个文本框提供一个错误(如果为空),并指定错误是针对哪个文本框的 *用开关编辑 private void Form1_Load(object sender, EventArgs e) { foreach (TextBox textBox in Controls) { textBox.Validating += textBox1_Validating; } } private void textBox1_Validat

我是否可以使用一个错误提供程序处理(例如)6个文本框的多个验证?

示例为每个文本框提供一个错误(如果为空),并指定错误是针对哪个文本框的

*用开关编辑

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); }
}

示例为每个文本框提供一个错误(如果为空),并指定错误是针对哪个文本框的

*用开关编辑

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); }
}

是的,有哈米德·塔莱比,看看这个StackOverflow问题@DJKRAZE,我看到了。但是一个问题!这样,我可以为每个文本框编写一个错误文本验证吗?例如:var controls=new[]{tx1,tx2….,txt10};foreach(controls.Where(e=>String.IsNullOrEmpty(e.Text)){errorProvider1.SetError(contro1,“请填写此”);errorProvider1.SetError(contro2,“请填写必填字段”);errorProvider1.SetError(contro3,“请填写全部”);}你能把你在评论中的代码添加到你的问题中吗?哈米德·塔莱比,如果你有代码,问一个关于某件事是否可行的问题有意义吗?试试你刚刚发布的示例代码。是的,哈米德·塔莱比,看看这个StackOverflow问题@DJKRAZE,我看到了。但是一个问题!这样我能写一个错误文本吗每个文本框的验证?例如:var controls=new[]{tx1,tx2….,txt10};foreach(控件中的var control.Where(e=>String.IsNullOrEmpty(e.text)){errorProvider1.SetError(contro1,“请填写此项”);errorProvider1.SetError(contro2,“请填写必填字段”);errorProvider1.SetError(contro3,“请填写全部”)}你能把你在评论中的代码添加到你的问题中吗?哈米德·塔莱比,如果你有代码,问一个关于某件事是否可行的问题有意义吗?试试你刚刚发布的示例代码。你不能用一种更有效的方式做这件事吗?还有,这是大量的硬编码。在控件上使用foreach循环可以减少代码顺便问一下,你能不能用一种更有效的方法来做这件事还有很多硬编码顺便说一下,在控件上使用foreach循环可以减少代码。。