我正在尝试使用此自定义方法在文本框中进行用户输入验证。但是我看到这里缺少一些东西,因为现在我无法将(焦点)移动到表单中的下一个文本框!
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;
}