c# - C#-验证时突出显示错误的控件

标签 c# winforms validation error-handling

我试图用try catch验证Windows窗体,到目前为止,我成功了。我的目标是,当有人忘记填补空白或输入错误时,使用警告提示捕获返回消息框。现在,我还要验证的每个控件上都有Validating事件,因此当有人将其保留为空或格式不正确时,它将在控件旁边显示错误。到目前为止,这似乎还可以(至少对我而言),但是我的问题是,如果用户甚至没有单击一个框,它只会显示消息框,但不会突出显示错误的控件。

下面是我的代码:

private void createButton_Click(object sender, EventArgs e)
    {
        try
        {
            Book newBook = new Book(titleBox.Text, authBox.Text, Convert.ToInt32(yearBox.Text), Convert.ToInt32(editBox.Text), pubComboBox.Text, descBox.Text);
            bookList.Add(newBook);
            booklistListBox.DataSource = bookList;
        }
        catch (FormatException)
        {
            MessageBox.Show("You probably missed a gap or put in incorrect form");
        }

    }

以及那些验证事件:
 private void titleBox_Validating(object sender, CancelEventArgs e)
    {
         if (titleBox.Text.Trim() == String.Empty)
        {
            errorProvider.SetError(titleBox, "Title is required");
            e.Cancel = true;
        }
         else
        {
            errorProvider.SetError(titleBox, "");
        }
    }

    private void authBox_Validating(object sender, CancelEventArgs e)
    {
        if (authBox.Text.Trim() == String.Empty)
        {
            errorProvider.SetError(authBox, "Author is required");
            e.Cancel = true;
        }
        else
        {
            errorProvider.SetError(authBox, "");
        }
    }

    private void yearBox_Validating(object sender, CancelEventArgs e)
    {
        if (yearBox.Text.Trim() == String.Empty)
        {
            errorProvider.SetError(yearBox, "Year is required");
            e.Cancel = true;
        }
        else
        {
            errorProvider.SetError(yearBox, "");
        }
    }

    private void editBox_Validating(object sender, CancelEventArgs e)
    {
        if (editBox.Text.Trim() == String.Empty)
        {
            errorProvider.SetError(editBox, "Edition is required");
            e.Cancel = true;
        }
        else
        {
            errorProvider.SetError(editBox, "");
        }
    }

    private void pubComboBox_Validating(object sender, CancelEventArgs e)
    {
        if (pubComboBox.Text.Trim() == String.Empty)
        {
            errorProvider.SetError(pubComboBox, "Publisher is required");
            e.Cancel = true;
        }
        else
        {
            errorProvider.SetError(pubComboBox, "");
        }
    }

    private void descBox_Validating(object sender, CancelEventArgs e)
    {
        if (descBox.Text.Trim() == String.Empty)
        {
            errorProvider.SetError(descBox, "Description is required");
            e.Cancel = true;
        }
        else
        {
            errorProvider.SetError(descBox, "");
        }
    }

那么,我不知道有什么方法可以通过按“创建”按钮来强制更改焦点或类似内容吗?
谢谢

最佳答案

尝试使用ValidateChildren():

private void createButton_Click(object sender, EventArgs e)
{
     bool gotIssues = this.ValidateChildren();
     if (gotIssues) 
     {
         // someone didn't validate well...
     }
}

关于c# - C#-验证时突出显示错误的控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20079087/

相关文章:

c# - "Encoded"SharePoint 客户端对象模型中的登录名

c# - 使用 C# 在 Blazor 中动态生成圆圈

c# - 如何知道外部应用程序是否已关闭?

php - 重复字段上特定于字段的错误

validation - 如何在输入验证中允许 UNICODE 代码点的子集?

validation - 如何在 struts 2 框架中验证选择标签

c# - 如何将每个功能或方法分配给一个按钮?

c# - 如何合并两个 Observable,以便在任何 Observable 完成时结果完成?

winforms - "BindingSource cannot be its own data source"- 尝试从另一个类中的方法重置绑定(bind)源时出错

c# - 文本框的覆盖文本属性无法正确刷新