Reputation: 3515
I want to validate winform using error provider. When user click on button multiple Validated methods are executed txtFieldOne_Validated(this, e); txtFieldTwo_Validated(this, e);
and I need solution to stop execution further if any of this validators fails and display error using error provider.
I thought to use private variable bool _formValid like
private btnValidateFields_Click(object sender, EventArgs e)
{
txtFieldOne_Validated(this, e);
txtFieldTwo_Validated(this, e);
if(_formValid)
{continue...}
}
private void txtFieldOne_Validated(object sender, EventArgs e)
{
if(....)
errorProvider1.SetError(txtFieldOne, "some error message")
_formValid = true;
else(....)
errorProvider1.SetError(txtFieldOne, "")
formValid = false;
}
but using this approach if last checked field was true than populated _formValid remains true and form pass.
Upvotes: 0
Views: 680
Reputation: 11687
I am not clear what you are trying to do. But as per your comments, I will suggest something like this.There is no need to call different Validation method for different controls. All controls should be validated in same method.
void IsFormValid(this, e)
{
bool result = ValidateAllControls();
if(!result)
return;
// Rest of the execution
}
bool ValidateAllControls()
{
if(!control1.IsValid)
return false;
if(!control2.IsValid)
return false;
if(!control3.IsValid)
return false;
return true;
}
Let me know if I misunderstood something.
Hope it helps.
Upvotes: 1