One-time Data Validity verification for winform forms

Source: Internet
Author: User
In Windows form programming, I do not know whether the tag attribute of the home widget is not.

Associated user-defined data. Each control has a tag attribute, which can be combined with the tag attribute and regular expression of the control.

Verify data validity. The method is as follows:



1. No matter what type of data is entered, all data can be input in the text box to develop data verification rules:



The tag attribute indicates the user-defined data associated with the control. It is used to verify all text boxes on the form at one time.

Control Data validity.



The tag attribute settings of the Textbox Control are as follows:



I. If you want to enter a number in Textbox, set the tag attribute of the Textbox Control to the following format:



1; Control name; 1; 0,999; 2;



Note:



1 indicates the number to be filled in



Control name: it can be the field name or the text of the label in front of the text box



1 indicates whether it is required. If it is not required, 0 is used.



0,999 indicates the value range of a number



2 indicates the number of decimal places



II. If you want to enter a string in Textbox, set the tag attribute of the Textbox Control to the following format:



2; Control name; 1; 1, 20;



Note:



2 indicates that the string is to be entered.



Control name: it can be the field name or the text of the label in front of the text box



1 indicates whether it is required. If it is not required, 0 is used.



1, 20 indicates the minimum and maximum length of the string



Iii. If the textbox is filled with a date, the tag attribute of the Textbox Control is set:



3; Control name; 1; 2000-1--1-1-1;



Note:



3 indicates the date to be filled in



Control name: it can be the field name or the text of the label in front of the text box



1 indicates whether it is required. If it is not required, 0 is used.



2000-1---1-1 indicates the allowed minimum date and maximum date



2. Implement a collection class syserrors (used to store all the errors returned during Control Data Validity verification)

), A syserrors class can contain multiple syserror (one generated when verifying the validity of the control data)

Errors) to implement the syserrors merge method (similar to the merge method of dataset, used to merge syse

Rrors, merge two error sets together) 3. Implement a validate class and implement the following in this class:

Method:
 

/**/ /// <Summary>

 

///Function: determines the validity of input data in a control.

 

///Principle: Data Validity is verified based on the tag attribute data specifications of the control,



All functions of the verification control call this function for verification.



Step: Determine the data type of input data in the control based on the tag attribute of the control,



Use regular expression matching to verify whether the tag attribute settings of the control are correct,



For controls whose strings should be entered:



Determine whether it is required;



Determines whether an invalid character exists;



Determines the maximum and minimum length of a character;



For controls whose strings should be entered:



Required or not;



Whether there are invalid characters (all numbers must be included here );



Maximum and minimum values allowed for verification;



Verify the number of decimal places;



Non-0 verification;



Controls for dates to be entered:



......



/**/ ///
// Control Reference
// returned error object

 

A. syserror validate (system. Windows. Forms. Control CTL)

 

/**/ ///   <Summary>

 

/// Function: determines the validity of input data of a control.

 

/// Principle: Data Validity is verified based on the tag attribute data specifications of the control,

 

/// Return the error to the error object set.

 

///   </Summary>

 

///   <Param name = "CTL"> Control Reference </Param>

 

///   <Returns> Error set object </Returns>

 

B. syserrors validatecontrol (system. Windows. Forms. Control CTL)

 

Public SYS. syserrors

Validatecontrol (system. Windows. Forms. Control CTL)

 

 

{

 

Syserrors errs =   New Syserrors ();

 

 

 

Switch (CTL. GetType (). tostring ())

 

{

 

Case   " System. Windows. Forms. groupbox " :

 

Foreach (System. Windows. Forms. Control

Ctlinner In CTL. Controls)

 

 

Errs. mergeerrors (validatecontrol (ctlinner )); // Recursion

 

Break ;

 

Case   " System. Windows. Forms. Panel " :

 

Foreach (System. Windows. Forms. Control

Ctlinner In CTL. Controls)

 

 

Errs. mergeerrors (validatecontrol (ctlinner )); // Recursion

 

Break ;

 

Default :

 

SYS. syserror err = Validate (CTL );

 

If (Err ! =   Null )

 

Errs. adderror (ERR );

 

Break ;

 

}

 

Return Errs;

 

}

 

/**/ ///   <Summary>

 

/// Function: determines the validity of data of all controls on a form.

 

/// Principle: Data Validity is verified based on the tag attribute data specifications of the control,

 

/// Return the error to the error object set.

 

///   </Summary>

 

C. syserrors validateform (system. Windows. Forms. Form FRM)

 

Public SYS. syserrors validateform (system. Windows. Forms. Form FRM)

 

{

 

If (_ Errors ! = Null ) _ Errors. Dispose ();

 

_ Errors = New Hewin. SYS. syserrors ();

 

Foreach (System. Windows. Forms. Control CTL In

FRM. Controls)

 

_ Errors. mergeerrors (validatecontrol (CTL ));

 

Return _ Errors;

 

}


4. Set the tag attribute for the control when you want to save data on the form, instantiate a syse
rrors object to collect all verification errors. Call the validateform method to write all the errors to S.
yserrors, if the Count attribute of syserrors is 0, the Code , otherwise, the displayerror method of the syser
RORs is called to display all errors to the user, you can also add the
code in the Add validate class to focus on the control with the first validity verification error on the form.

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.