Here is one approach:
private List<Control> m_lstControlsToValidate; private void SetupControlsToValidate() { m_lstControlsToValidate = new List<Control>(); //Add data entry controls to be validated m_lstControlsToValidate.Add(sometextbox); m_lstControlsToValidate.Add(sometextbox2); } private void ValidateSomeTextBox() { //Call this method in validating event. //Validate and set error using error provider } Private void Save() { foreach(Control thisControl in m_lstControlsToValidate) { if(!string.IsNullOrEmpty(ErrorProvider.GetError(thisControl))) { //Do not save, show messagebox. return; } } //Continue save }
EDIT:
For each control in m_lstControlsToValidate, you need to write a validation method that will be run in the Validating event.
ErrorProvider.GetError (thisControl) will return some errortext or emptystring. An empty line means that the control is in order. In addition, the control contains some error, and we abort the save operation.
We do this on all controls in m_lstControlsToValidate. If all controls are error free, we continue with the cancellation of the cancellation.
source share