Is there a shortcut in validating fields in winforms? For example a particular textBox is required to be filled up before saving a record. What I always do is I check first all the required fields programatically before saving. Example:
protected bool CheckFields()
{
bool isOk = false;
if(textBox1.Text != String.Empty)
{
isOk = true;
}
return isOk;
}
private void btnSave_Click(object sender, EventArgs e)
{
if(CheckFields())
{
Save();// Some function to save record.
}
}
Is there a counter part of Validator in ASP.Net in winforms? Or any other way around…
Here is one approach:
EDIT:
For each control in m_lstControlsToValidate you need to write the validation method that would be fired in Validating event.
ErrorProvider.GetError(thisControl) will return some errortext or emptystring. Empty string means the control is fine. Else the control contains some error and we abort the save operation.
We do this on all the controls in m_lstControlsToValidate. If all controls are error free we continue with save else abort.