I have a C# winform project that I want to validate a form before closing.
The Validating event occurs when a control yields focus to another control.
So the event occurs when you use the tab key to move away from the current control or when you click another control with the mouse1.
3) If the control's content is not valid, pass it to the Error Provider's Set Error method along with your validation message.
4) Depending on the valid state, let the form close (Dialog Result = OK) or keep the form open (Dialog Result = None).
Sure enough, clicking OK or pressing the Enter key will run the Validating event I wired up to the Text Box.
Pressing the Cancel button bypasses Validating, which is perfect. Add(panel); In this simplified example the textbox will not let you proceed if there are 3 characters input.
control is Combo Box and check if it's dropped down and if so bail out.
Neither the Process Dialog Keys nor Validating handler answers worked for me, perhaps because I'm using an error Provider instead of a Message Box.
So I have an event that fires for that like so: I only want to close the form if the validation is successful. However, I don't want to have the validation run when the textboxes lose focus, only when the whole form is being validated. I have been using the Error Provider class to support this visually.
Each control that I want to be validated, I have registered with the "validating" event. So basic question is, what is the best approach to validate a specific set of controls only when I want to and not when focus is lost from the control? The "Auto Validate" property is what I was looking for (found in link you provided).
And I find that extremely annoying, just to give an example, if I open this winform, and then immediately try to close it, it is not going to let me, because the validation will trigger complaining that the first textbox have no text entered.