There are many controls on the form to set some parameters. You need to verify all parameters when you click OK. If any parameter is invalid, an error message is displayed. The effect is as follows:
First, you must add a verification event for the control.
Private void textboxincluvalidated (Object sender, eventargs e) <br/>{</P> <p> If (string. isnullorempty (textbox1.text) <br/> trim (textbox1, "invalid textbox value, cocould not be null"); <br/> else <br/> errorprovider1.seterror (textbox1, String. empty); <br/>}
Private void textbox2_validated (Object sender, eventargs e) <br/>{< br/> If (string. isnullorempty (textbox2.text) <br/> errorprovider1.seterror (textbox2, "textbox2 invalid"); <br/> else <br/> errorprovider1.seterror (textbox2 ,""); <br/>}
Under normal circumstances, the validated event is triggered only when the focus of the control changes. The following are some information about msdn.
When you change the focus by using the keyboard (tab, Shift + tab, and so on), by calling the select or selectnextcontrol methods, or by setting the containercontrol. activecontrol property to the current form, focus events occur in the following order:
Enter
Gotfocus
Leave
Validating
Validated
Lostfocus
When you change the focus by using the mouse or by calling the focus Method, focus events occur in the following order:
Enter
Gotfocus
Lostfocus
Leave
Validating
Validated
If the causesvalidation property is setFalse,ValidatingAndValidatedEvents are suppressed.
If the cancel property of the canceleventargs is setTrueInValidatingEvent Delegate, all events that wocould usually occur afterValidatingEvent are suppressed.
For more information about handling events, see Consuming events.
However, due to the large number of controls, the Focus cannot be changed one by one, and it is unreasonable to do so. Is there a simple way to trigger the validated events of these controls?
I found the solution on the Internet and found that the solution is actually very simple. The Form class provides a validatechildren method, which can be used to easily implement the above requirements.
Data http://msdn.microsoft.com/en-us/library/ms159409.aspx on msdn
. NET Framework class libraryform. validatechildren Method
This member overrides containercontrol. validatechildren (), and more complete documentation might be available in that topic.
Causes all of the child controls within a control that support validation to validate their data.
Namespace:System. Windows. Forms
Assembly:System. Windows. Forms (in system. Windows. Forms. dll)
C #
[Browsableattribute (true)]
Public override bool validatechildren () Return Value
Type: system. Boolean
True if all of the children validated successfully; otherwise, false. If called from the validating or validated event handlers, this method will always return false. Platforms
If you do not want to verify all controls, for example, some controls may change the visible attribute at any time, you can skip these controls during verification by passing parameters when validatechildren is called.
Public Virtual bool validatechildren (
Validationconstraints
)
This. validatechildren (validationconstraints. Visible)
Validationconstraints enumeration members
|
Member name |
Description |
|
Enabled |
Validates child controls whoseEnabledProperty is setTrue. |
|
Immediatechildren |
Validates child controls that are directly hosted within the container. does not validate any of the children of these children. For example, if you have a form that contains a custom usercontrol, andUsercontrolContains a button, usingImmediatechildrenWill causeValidatingEvent ofUsercontrolTo occur, but notValidatingEvent ofButton. |
|
None |
Validates all child controls, and all children of these child controls, regardless of their property settings. |
|
Selectable |
Validates child controls that can be selected. |
|
Tabstop |
Validates child controls that have a tabstop value set, which means that the user can navigate to the control using the tab key. |
|
Visible |
Validates child controls whose visible property is setTrue. |