C# 是否使用已验证事件清除由errorProvider标记的错误?

C# 是否使用已验证事件清除由errorProvider标记的错误?,c#,validation,errorprovider,C#,Validation,Errorprovider,第一个帖子!我是C#and errorProvider的新手。我在四处寻找使用errorProvider的最佳实践。我找到了以下代码: void TextBox_Validating( object sender, CancelEventArgs e ) { TextBox textBox = sender as TextBox; bool valid = textBox.TabIndex == 1 || textBox.Text.Length > 0; if(

第一个帖子!我是C#and errorProvider的新手。我在四处寻找使用errorProvider的最佳实践。我找到了以下代码:

void TextBox_Validating( object sender, CancelEventArgs e ) {
    TextBox textBox = sender as TextBox;

    bool valid = textBox.TabIndex == 1 || textBox.Text.Length > 0;

    if( !valid )
        m_ErrorProvider.SetError( textBox, "Error " + textBox.Name );

    e.Cancel = !valid;
}

private void TextBox_Validated(object sender, System.EventArgs e) {
    TextBox textBox = sender as TextBox;
    m_ErrorProvider.SetError(textBox, "");
}
我没有处理已验证事件,而是简单地在进入验证事件的过程中清除错误,并让错误在该事件处理程序中再次设置,如下所示:

void TextBox_Validating( object sender, CancelEventArgs e ) {
    TextBox textBox = sender as TextBox;

    m_ErrorProvider.SetError(textBox, "");

    bool valid = textBox.TabIndex == 1 || textBox.Text.Length > 0;

    if( !valid )
        m_ErrorProvider.SetError( textBox, "Error " + textBox.Name );

    e.Cancel = !valid;
}
我的验证比这更复杂,因为我有几个地方需要清除。我的背景是这种技术很常见的嵌入式代码


处理已验证事件是否是更好的做法?谢谢。

没关系。你可以看看。您也可以考虑为模型类实现。此外,您可能会发现这篇msdn文章很有帮助:没关系。你可以看看。您也可以考虑为模型类实现。此外,您可能会发现这篇msdn文章很有帮助: