|
Hi, so I have multiple textboxes on my c# winform and I am looking for a method to validate each box.
My current implementation works, however I am trying to find a way to expand on it and report which textboxes are not validating successfully.
So say return a string or array where i can search through and then set an errorprovider on them.
This is my validation method.(The method is void and not a bool because i want to return a variable with the failed textboxes)
public bool Successful { get; set; }
public string Information { get; set; }
public void ValidateText(List<object> textbox)
{
foreach (object obj in textbox)
{
if (obj == "")
{
Successful = false;
}
else
{
Successful = true;
}
}
}
and here is my main code
List<object> textbox = new List<object>();
textbox.Add(FirstNTxt.Text);
textbox.Add(LastNTxt.Text);
textbox.Add(CourseTxt.Text);
FormValidation validate = new FormValidation();
validate.ValidateText(textbox);
if(!validate.Successful)
{
MessageBox.Show("error");
}
else
{
}
Any assistance will be appreciated, thanks.
modified 11-Mar-21 21:01pm.
|
|
|
|
|
Your ValidateText will set the Successful to indicate if the last entry in the textbox list is valid or not. All the checking of the earlier entries is lost.
Try something like:
public void ValidateText(List<TextBox> textboxes)
{
Successful = true;
foreach (var tb in textboxes)
{
if (string.IsNullOrEmpty(tb.Text))
{
Successful = false;
}
}
}
and then
List<TextBox> textboxes = new List<TextBox>();
textboxes.Add(FirstNTxt);
textboxes.Add(LastNTxt);
textboxes.Add(CourseTxt);
FormValidation validate = new FormValidation();
validate.ValidateText(textboxes);
if(!validate.Successful)
{
MessageBox.Show("error");
}
else
{
}
Personally, I'd probably make a validation method that operates on one string at a time and then validate and set the errorprovider directly for each TextBox one at a time. (Despite what I showed above, I wouldn't pass the actual TextBox references around. The method should validate the string, possibly returning an error message. The caller (form) passes the .Text and reports the error appropriately.)
|
|
|
|
|
This code well search for all text box in your form and validated it, and it will be better If you Use Tag for Description of Textbox so the user know what he is missing, Plus you avoid typing in the MessageBox something like "No Name" , No Phone" , etc ...
More Over you can tell which TextBox in the form is needed to be validate by checking that Tag if it's Empty or not.
private void CheckTextBox()
{
TxtName.Tag = "First Name";
TxtPhone.Tag = "Phone Number";
foreach (Control control in this.Controls)
{
if (control.GetType().Equals(typeof(TextBox)))
{
if (ValidateText(control.Text))
{
MessageBox.Show(control.Tag.ToString());
MessageBox.Show(control.Name);
}
}
}
}
private bool ValidateText(string text)
{
return text.Equals(string.Empty);
}
P.S :
You can assign the Tag for TextBox by properties Windows during design time.
|
|
|
|
|
Two fantastic suggestions. Thank you so much. Both of these solve the issue I was facing.
Once again, Thanks!
modified 11-Mar-21 21:01pm.
|
|
|
|
|
Sorry, i just got around to trying both methods. For the first one,
public void ValidateText(List textboxes)
{
Successful = true;
foreach (var tb in textboxes)
{
if (string.IsNullOrEmpty(tb.Text))
{
Successful = false;
}
}
}
how do i go about outputting the actual control name that failed? all i can do is echo tb.text which ofc is empty. Any help would be great.
modified 11-Mar-21 21:01pm.
|
|
|
|