CausesValidation
System.Windows.Forms.Control.CausesValidationプロパティは、そのコントロールがフォーカスを得た際にフォーカスを失ったコントロールのValidatingイベントが発生するか、しないかを制御するはずなのだが、
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button1;
private void InitializeComponent()
{
this.button1.CausesValidation = false;
〜
}
private void button1_Click(object sender, EventArgs e)
{
this.Close();
}
private void textBox1_Validating(object sender, CancelEventArgs e)
{
MessageBox.Show("Validating!!");
}
こんな感じの最小構成でテストしたのだが、button1.CausesValidation=falseとしているにもかかわらず、実行してButton1を押下すると何度試してもValidatingイベントが発生してしまう。
Validatingイベントを抑制するには、イベントハンドラで
private void textBox1_Validating(object sender, CancelEventArgs e) { if ( Form.ActiveForm.ActiveControl != null ) { if (!Form.ActiveForm.ActiveControl.CausesValidation) { return; } } MessageBox.Show("Validating!!"); }
みたいに書くしかないんだが。
これは私がCausesValidationの意味を履き違えているのか、それとも仕様なのか(仕様だとしたらあまりに間抜けだが)はたまたバグなのだろうか。
追記:やはり仕様なのか?