Validating several textboxes on a C# windows form at the same time
Asked Answered
B

4

10

I have a form with several textboxes and other controls. I'm using the errorprovider control and I wired the validating event on each textbox that I need to validate. 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. 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.

The behavior I want is using the errorprovider, how can I validate the whole form only when I click the Save button of the form?

Thanks

Besought answered 24/12, 2009 at 16:57 Comment(0)
E
10

Check the property Form.AutoValidate.

Possible values:

  • Disable

Implicit validation will not occur. Setting this value will not interfere with explicit calls to Validate or ValidateChildren.

  • EnablePreventFocusChange

Implicit validation occurs when the control loses focus.

  • EnableAllowFocusChange

Implicit validation occurs, but if validation fails, focus will still change to the new control. If validation fails, the Validated event will not fire.

Setting it to EnableAllowFocusChange will resolve the problem of opening the form and immediatly trying to close it since the focus will be transferred to the Cancel button.

If you want complete control over the validation you can set it to Disable and perform manual validation using Form.Validate and Form.ValidateChildren. I am not 100% sure but I believe that Form.ValidateChildren will not trigger validation events for controls placed inside a TabControl.

Etesian answered 24/12, 2009 at 17:24 Comment(0)
J
1

Set the CausesValidation property on all the controls (except the Save button) to False. This will suppress the Validating event for all controls except the button. It might not be the exact behvior you want, as the annoying behavior will happen when you change focus on the button, but it's close to what you want I think.

Another option, if that doesn't work, is to loop through the controls when the button is clicked and validate them manually one at a time...

Here's the MSDN link: http://msdn.microsoft.com/en-us/library/system.windows.forms.control.causesvalidation.aspx

Julie answered 24/12, 2009 at 17:23 Comment(0)
M
1

Just to keep this updated as João Angelo said the Form.ValidateChildren() will not trigger the validation of controls inside let's say a GroupBox or TabControl.

However, you can pass ValidationContraints to the overloaded ValidateChildren(ValidationConstraints) method and set it to None which:

"Validates all child controls, and all children of these child controls, regardless of their property settings."

Meeker answered 2/6, 2011 at 15:57 Comment(0)
P
0

There isn't any way to get WinForms to validate controls on save. Unlike a web form, there isn't any "submit" or "save" event (at least not on the form). You can program the form to behave this way, but you won't be able to use data binding (Save would set the model and check for errors).

Perhaps a better way is to not use a dialog to show errors (I'm assuming you are since you are saying this is a problem with closing the form). Tooltips can provide a better user experience. If the user ignores the tooltips, you can provide a dialog when they hit Save. They will be able to close the form even if there is bad data.

Peele answered 24/12, 2009 at 17:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.