我在winforms,c#应用程序的选项卡控件中创建了所有文本框的列表。现在我想要做的是检查列表中的每个文本框并测试以查看该文本框是否为空。如果是我想使用错误提供程序让用户知道它需要填充。这是我现在使用的代码。如何使用文本框控件列表测试文本框为空?
//gathers the controls and adds them to a list on the main form
public static List<Control> GetControls(Control form, string type)
{
var controlList = new List<Control>();
foreach (Control childControl in form.Controls)
{
if (childControl.Name.Contains(type))
{
controlList.Add(childControl);
}
if (childControl.HasChildren) GetControls(form, type);
}
return controlList;
}
//Created in a class called DataVerification
public bool IsNotEmpty(string value, out string errorMessage)
{
errorMessage = "";
if (value.Length == 0)
{
errorMessage = "This Field Is Required";
return false;
}
return false;
}
//method that uses the DataVerification class and the error provider
public void IsNotEmpty()
{
availableControls = GetControls(this.tabPage1, "Text");
string errorMessage;
DataVerification verify = new DataVerification();
foreach (Control c in availableControls)
{
if (!verify.IsNotEmpty(c.Text, out errorMessage))
{
ErrorProvider.SetError(c, errorMessage);
}
}
}
即使我可以只显示一个消息框,让用户知道哪些文本框是空的就可以了。任何帮助,将不胜感激。提前致谢。