初投稿!私は 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 イベントの処理はより良い方法ですか? ありがとう。