Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is my validating event not firing in C#?

Tags:

I have a form that is dynamically created. It is a winForms application.

This form is just a menu and a series of textboxes and labels. For the sake of this example, you can ignore the labels.

My problem is: When I edit stuff in the textboxes, then click the menu to issue "Save", the text from the last text box still hasn't issued its "Validating" method. This appears to be because the control still has focus, and that the menu is in a separate thread.

How can I force the validating events to fire when the user clicks "save"? I don't know which text box the user is on (if any), and issuing SomeKnownControl.Focus(); within the "save" function doesn't seem to help.

like image 782
Jerry Avatar asked Feb 17 '09 20:02

Jerry


People also ask

What is the difference between validating and validated in C#?

Validating is fired just before the validation process starts, this is the place to put the actual validation code. Validated is fired after the validation process has finished and is designed to be the place to something based on the validation result.

What is the importance of the validating event in C#?

The Validating event is the ideal place for storing the field-level validation logic for a control. The event handler for validating the event receives an argument of type CancelEventArgs. Its only property, Cancel, cancels the event when it is set to true.


1 Answers

I've had this before. In your form:

private void SaveButtonClick(...)
{
    if (this.ValidateChildren())
    {
        // do save
    }
}

ValidateChildren on MSDN

like image 63
geofftnz Avatar answered Sep 23 '22 04:09

geofftnz