多个文本框验证



所以我有一个表格,里面有很多TextBox-es,都需要填写。我研究过文本框验证,但只能找到验证单个文本框的说明。下面是我为奇异文本框验证准备的代码。我只是想知道是否有可能一次击中所有人,而不是每个人都这样。任何帮助都将不胜感激!

private void txtName_Validating(object sender, CancelEventArgs e)
{
if (string.IsNullOrEmpty(txtName.Text.Trim()))
{
epName.SetError(txtName, "Name is required.");
}
else
{
epName.SetError(txtName, string.Empty);
}
}

假设您使用的是WinForms

// Get all the controls of the forms
var controls = this.Controls;
foreach (Control mycontrol in controls)
{
// Check if the Control is a TextBox
if (mycontrol is TextBox)
{
//Perform Operation
}
}
var controls = this.Controls;
foreach (Control mycontrol in controls)
{
// Check if the Control is a TextBox
if (mycontrol is TextBox)
{
epname.seterror(mycontrol, mycontrol+"is required");
}
}

最新更新