C # Validator (data verification) of controls in Windows Form)

Source: Internet
Author: User

Due to an occasional idea, I made a living to make A Validator control under windows form, or simply say a class!
Because the Validator control in webform is too easy to use. Haha, read the code directly!
 
The following class is mainly a simple verification class, but it serves only as an example. Let's take full advantage of other functions!
1 using System;
2 using System. Collections. Generic;
3 using System. Linq;
4 using System. Text;
5 using System. Windows. Forms;
6
7 namespace FinanceManager. Code
8 {
9 /// <summary>
10 // input verification class
11 /// </summary>
12 public class Validator
13 {
14 /// <summary>
15 // determine whether the content of the specified text box meets the conditions
16 /// </summary>
17 /// <param name = "textBox"> text box to be verified </param>
18 /// <param name = "type"> specify the verification type </param>
19 /// <param name = "LabelDisplayError"> the label of the error must be displayed. </param>
20 /// <param name = "lenth"> used to verify the length of Characters in the text box </param>
21 /// <returns> If the condition is met, True is returned. If the condition is not met, False is returned. </returns>
22 public static Boolean ValidTextBox (TextBox textBox, ValidType type, Label LabelDisplayError, Int32 lenth = 10)
23 {
24 if (LabelDisplayError = null)
25 {
26 return ValidTextBox (textBox, type, lenth );
27}
28 else
29 {
30 if (! ValidTextBox (textBox, type, lenth ))
31 {
32 LabelDisplayError. Visible = true;
33 return ValidTextBox (textBox, type, lenth );
34}
35 else
36 {
37 LabelDisplayError. Visible = false;
38 return ValidTextBox (textBox, type, lenth );
39}
40}
41}
42 // <summary>
43 // determine whether the content of the specified text box meets the conditions
44 /// </summary>
45 // <param name = "textBox"> text box to be verified </param>
46 // <param name = "type"> specify the verification type </param>
47 // <param name = "lenth"> used to verify the length of Characters in the text box </param>
48 /// <returns> If the condition is met, True is returned. If the condition is not met, False is returned. </returns>
49 public static Boolean ValidTextBox (TextBox textBox, ValidType type, Int32 lenth = 10)
50 {
51 Boolean flag = false;
52 switch (type)
53 {
54 case ValidType. Required:
55 if (! String. IsNullOrEmpty (textBox. Text. Trim ()))
56 {
57 flag = true;
58}
59 else
60 {
61 flag = false;
62}
63 break;
64 case ValidType. CharLength:
65 if (! String. IsNullOrEmpty (textBox. Text. Trim ()))
66 {
67 if (textBox. Text. Trim (). Length <lenth)
68 {
69 flag = true;
70}
71 else
72 {
73 flag = false;
74}
75}
76 else
77 {
78 flag = false;
79}
80 break;
81 case ValidType. EnglishChar:
82 if (! String. IsNullOrEmpty (textBox. Text. Trim ()))
83 {
84 foreach (Char c in textBox. Text. Trim (). ToLower ())
85 {
86 if (! (C> = 'A' & c <= 'Z '))
87 {
88 flag = false;
89}
90}
91 flag = true;
92}
93 else
94 {
95 flag = false;
96}
97 break;
98 case ValidType. Number:
99 if (! String. IsNullOrEmpty (textBox. Text. Trim ()))
100 {
101 Int32 I = 0;
102 if (Int32.TryParse (textBox. Text. Trim (), out I ))
103 {
104 flag = true;
105}
106 else
107 {
108 flag = false;
109}
110}
111 else
112 {
113 flag = false;
114}
115 break;
116}
117
118 return flag;
119}
120}
121 /// <summary>
122 // verification type
123 /// </summary>
124 public enum ValidType
125 {
126 /// <summary>
127 // indicates required items
128 /// </summary>
129 required,
130 /// <summary>
131 /// indicates that the item must be a number.
132 /// </summary>
133 Number,
134 /// <summary>
135 /// indicates that the item must be an English character
136 /// </summary>
137 EnglishChar,
138 /// <summary>
139 /// indicates that the item must be of the specified length
140 /// </summary>
141 CharLength
142}
143}
 

So, I used it at the user login location. This code,
View my interface:



 

The layout of controls is like this, mainly to verify our functions! (Don't hit me with eggs in the back of the kids shoes ~)

The user name and password are required. To test the password, click "User Logon". The following interface is displayed:



 

How is it? Can I see the asterisk next to the user name? Just look! It's similar to validator in webform! What if I enter the user name without the password?



 

Haha, it looks very useful. Now let's look at the background code,
1 using System;
2 using System. Collections. Generic;
3 using System. ComponentModel;
4 using System. Data;
5 using System. Drawing;
6 using System. Linq;
7 using System. Text;
8 using System. Windows. Forms;
9 using FinanceManager. Code; // reference the above verification class
10
11 namespace FinanceManager
12 {
13 public partial class frmLogin: Form
14 {
15 public frmLogin ()
16 {
17 InitializeComponent ();
18}
19
20 private void button#click (object sender, EventArgs e)
21 {
22 if (! Validator. ValidTextBox (txtUserName, ValidType. Required, lblErrorName ))
23 {
24 MessageBox. Show ("the user name is required. Enter the user name! "," System prompt ", MessageBoxButtons. OK, MessageBoxIcon. Error );
25 txtUserName. Focus ();
26}
27 else if (! Validator. ValidTextBox (txtPassword, ValidType. Required, lblErrorPassword ))
28 {
29 MessageBox. Show ("password is required. enter your password! "," System prompt ", MessageBoxButtons. OK, MessageBoxIcon. Error );
30 txtPassword. Focus ();
31}
32 else
33 {
34 MessageBox. Show ("congratulations, logon successful! "," System prompt ", MessageBoxButtons. OK, MessageBoxIcon. Information );
35 frmMain main = new frmMain ();
36 main. Show ();
37 this. Hide ();
38}
39}
40}
41}

The code is clear and easy to understand! I hope you will be inspired. This can also be made into attributes, or you can expand the textbox Control by yourself. All kinds of methods can be implemented. The main point is to explain, sharing more code makes our path wider and farther!

From Hgates

Related Article

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.