How to automatically trigger the validated event of the control on the form

Source: Internet
Author: User

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:

  1. Enter

  2. Gotfocus

  3. Leave

  4. Validating

  5. Validated

  6. Lostfocus

When you change the focus by using the mouse or by calling the focus Method, focus events occur in the following order:

  1. Enter

  2. Gotfocus

  3. Lostfocus

  4. Leave

  5. Validating

  6. 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.

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.