3

I have a little form with some text boxes and combo boxes, each with their own validating event which just fills the default value either 1 or 0 depending on the box when the user moves on to the next box without entering anything, however i also want to run all the validations when the user just clicks on the submit button directly.

private void Validating_Zero(object sender, CancelEventArgs e)
{
    if (((TextBox_Pro)sender).Text == "")
    {
        ((TextBox_Pro)sender).Text = "0";
    }
}

private void Validating_One(object sender, CancelEventArgs e)
{
    if (((TextBox_Pro)sender).Text == "")
    {
        ((TextBox_Pro)sender).Text = "1";
    }
}

private void Start_Validating(object sender, CancelEventArgs e)
{

}
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
DeadlyTitan
  • 35
  • 1
  • 5
  • to which event are the `Validating_...` handler hooked? `LostFocus` ? – Mong Zhu Sep 07 '17 at 14:13
  • They are hooked to the Validating event of the text boxes, so they fire up when the user is about to leave the text box and move to the next text box. – DeadlyTitan Sep 07 '17 at 18:48

1 Answers1

6

Calling ValidateChildren() method of the form, cause all of the child controls of the form to validate their data.

You can also use the the other overload of the method which accepts a validation constraint, ValidateChildren(ValidationConstraints) to limit the validation to immediate child controls, enabled controls, tab stop controls, selectable controls, visible controls or all control.

To see an example of validation using Validating event and Error provider control and showing error summary and also read more about validation options in Windows Forms, take a look at these posts:

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • 1
    what a great answer, thank you I learned something new. I will delete my answer, since it became superfluous. :) – Mong Zhu Sep 09 '17 at 18:43