Input verification control in the Win Form program

Source: Internet
Author: User

This article from: http://www.cnblogs.com/happy5630/articles/1257075.html

 

ASP. NET provides a mechanism to verify user input before the user submits data. However, in the WinForm program, Microsoft does not provide such a component. The purpose of this article is to provide a WinForm verification control, and you can perform verification without writing any code.

Previous solutions
When we verify the text in a control, for example, to verify a TextBox named txtPassword, the user is required to enter a letter starting with 3-8 digits or letters. We have to register the Validating event of txtPassword:

private void txtPassword_Validating(object sender, CancelEventArgs e){if (string.IsNullOrEmpty(txtPassword.Text)){errorProvider1.SetError(txtPassword, "Password required!");}else if (!Regex.IsMatch(txtPassword.Text, @"[A-Za-z][A-Za-z0-9]{2,7}")){errorProvider1.SetError(txtPassword, "Password invalid!");}else{errorProvider1.SetError(txtPassword, null);}}

In the submit button, you need to write the following code:

 

private void btnOK_Click(object sender, System.EventArgs e){foreach (Control control in this.Controls){// Set focus on control        control.Focus();// Validate causes the control's Validating event to be fired,        // if CausesValidation is True        if (!Validate()){DialogResult = DialogResult.None;return;}}}

This is really a stupid method. If there are many controls on your interface, it will bring a lot of trouble to the maintenance.

Background
I have gained a lot of inspiration from the following article. You can also read it:
Extending Windows Forms with a Custom Validation Component Library-Michael Weinhardt.

Code usage
First create a win Form application, and then follow the steps below to use this control.

1. Add controls

Add the Item in Toolbox, select Browse, and select the Link Library of the Validator. dll Control.

Then it is added to the toolbar.

If you have set a strong type for the verification control and add it to GAC. You can also select to load from ". NET Framework components.

2. Add and configure Components

Drag the Validator control to form.
Its most important attributes are:
Form: indicates the form to be verified by the verification control. It registers the FormClosing event and automatically verifies it when it is disabled.
Mode: it is an enumeration type attribute and can be one or a combination of FocusChange and Submit. FocusChange means that if verification fails, the focus will be set on that control or moved to the next tab Index Control. Submit means that when the form event is closed, it is blocked. If the verification fails, it is disabled.
BlinkRate, BlinkStyle, Icon, RightToLeft: the attribute is the same as that of ErrorProvider. Please refer to the MSDN help documentation.

3. Configure the text box and attributes

Now we assume there are three TextBox and one submit button.

 

Name Text Properties Function
txtName     User name, required input.
txtPassword   PasswordChar= "*" Input password, begins with an alphabet and has 3-8 numbers or alphabets.
txtRePassword   PasswordChar= "*" Re-input password, same as password.
txtSubmit Submit DialogResult= OK Submit and close form.

 

Next, let's configure and verify these controls. We use txtRePassword for Demonstration:
The txtRePassword attribute window is displayed. You are surprised to find that there are multiple directory groups Validation:
Type: drop-down "Type on validator1" and select Required and Compare

RequiredMessage: Enter the prompt message "Please input re-password .".
ComparedControl: Select "txtPassword" from the drop-down list ".
CompareOperator: Select "Equal ".
CompareMessage: Enter "Re-Password not same as password ".

Other property settings:

Name Validation
txtName Type= Required
txtPassword Type=Required|RegularExpression
RegularExpression= ^ [A-Za-Z] [A-Za-z0-9] {} $
RegularExpressionOptions=None
txtRePassword Type =Required|Compare
ComparedControl=txtPassword
CompareOperator=Equal

 

Test your form
Run it. You must have found something different :)

Thanks

 

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.