テキスト ボックスでのユーザー入力の検証にこのカスタム メソッドを使用しようとしています。しかし、フォームの次のテキスト ボックスに移動 (フォーカス) できないため、ここに何かが欠けています。
private void textBox_Validating(object sender, CancelEventArgs e)
{
TextBox currenttb = (TextBox)sender;
if (currenttb.Text == "")
{
MessageBox.Show(string.Format("Empty field {0 }", currenttb.Name.Substring(3)));
e.Cancel = false;
}
else
{
e.Cancel = true;
}
}
フォーム コンストラクターで foreach ループを使用してハンドラーをテキスト ボックスに追加します。
foreach(TextBox tb in this.Controls.OfType<TextBox>().Where(x => x.CausesValidation == true))
{
tb.Validating += textBox_Validating;
}