Reputation: 57
I have created a list of the all the textboxes on a tab control in my winforms, c# application. Now what I would like to do is check each textbox in the list and test to see if that textbox is empty. If it is I would like to use the error provider to let the user know it needs to be filled. Here is the code I am using now.
//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);
}
}
}
Even if I could just display a messagebox that lets the user know which textboxes are empty would be fine. Any help would be appreciated. Thanks in advance.
Upvotes: 0
Views: 1251
Reputation: 11
The method IsNotEmpty
is a boolean one; always return false.
public bool IsNotEmpty(string value, out string errorMessage)
{
errorMessage = "";
if (value.Length == 0)
{
errorMessage = "This Field Is Required";
return true; //instead of false
}
return false;
}
Upvotes: 1
Reputation: 1728
I'm not sure I understand the question, but if you want to know where the TextBox
controls are and verify only these, you can do the following:
foreach (Control c in availableControls)
{
TextBox t = c as TextBox;
if (t != null && !verify.IsNotEmpty(t.Text, out errorMessage))
ErrorProvider.SetError(t, errorMessage);
}
Upvotes: 0
Reputation: 3644
I don't exactly understand what your question is...
What you can use to test if a textbox's value is empty is the String.IsNullOrEmpty();
Instead of:
if (!verify.IsNotEmpty(c.Text, out errorMessage))
{
ErrorProvider.SetError(c, errorMessage);
}
Just do this:
if (String.IsNullOrEmpty(c.Text))
{
ErrorProvider.SetError(c, "This field is required!");
}
Also, I recommend you change your naming scheme a tad. It's good practice to have methods and such be in the 'truthful' form. So instead of IsNotEmpty, IsEmpty would be easier to read/maintainable. That line of !verify.IsNotEmpty()
throws a double negative!
EDIT: As ahmad said: your method always returns false. It should read:
public bool IsNotEmpty(string value, out string errorMessage)
{
if (value.Length == 0)
{
errorMessage = "This Field Is Required";
return false;
}
return true;
}
Upvotes: 0
Reputation: 3653
You can cast the control to a text-box in this manner:
TextBox t = (TextBox)c;
and then use the t.Text to perform validation.
Upvotes: 0