第一篇文章!我是C#和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, "");
}
我没有处理Validated事件,而是简单地在进入Validating事件的过程中清除错误,并在该事件处理程序中再次设置错误,如下所示:
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;
}
我的验证比这更复杂,我有几个地方可以清除它。我的背景是嵌入式代码,这种技术很常见。
处理Validated事件更好吗?感谢。