C# Windows Form下的控件的Validator(数据验证)

由于偶尔的一个想法,谋生了一个做一个windows form下的Validator控件,或者直接说类吧!

因为webform下的Validator控件太好用了。哈哈,直接看代码!

 

下面这个类,主要是一个简单的验证类,不过只是起到一个抛砖引玉的作用,其他的功能,大家发挥想象吧!

  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 /// 输入验证类
11 /// </summary>
12 public class Validator
13 {
14 /// <summary>
15 /// 判断指定文本框内容是否满足条件
16 /// </summary>
17 /// <param name="textBox">需要验证的文本框</param>
18 /// <param name="type">指定验证类型</param>
19 /// <param name="LabelDisplayError">需要显示错误的标签</param>
20 /// <param name="lenth">用于验证文本框内字符的长度</param>
21 /// <returns>满足条件返回True,不满足条件返回False</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 /// 判断指定文本框内容是否满足条件
44 /// </summary>
45 /// <param name="textBox">需要验证的文本框</param>
46 /// <param name="type">指定验证类型</param>
47 /// <param name="lenth">用于验证文本框内字符的长度</param>
48 /// <returns>满足条件返回True,不满足条件返回False</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 /// 验证类型
123 /// </summary>
124 public enum ValidType
125 {
126 /// <summary>
127 /// 表示必须填写的项
128 /// </summary>
129 Required,
130 /// <summary>
131 /// 表示必须为数字的项
132 /// </summary>
133 Number,
134 /// <summary>
135 /// 表示必须为英文字符的项
136 /// </summary>
137 EnglishChar,
138 /// <summary>
139 /// 表示必须为指定长度的项
140 /// </summary>
141 CharLength
142 }
143 }


那么,我就在用户登录的地方用到了,这个代码、

看看我的界面:

控件布局就这样吧,主要是为了验证我们的功能!(后面童鞋,别拿鸡蛋砸我~)

我设置的是:用户名和密码必填,现在我为了测试,我直接点击“用户登录”按钮,出现以下界面:

怎么样?看见用户名后面那个星号了吧?顺眼吧!跟webform下的validator差不多吧!呵呵,我填写用户名,不写密码看看,如何?

呵呵,貌似很管用,现在我们看看后台代码、

 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; //引用上面验证类
10
11 namespace FinanceManager
12 {
13 public partial class frmLogin : Form
14 {
15 public frmLogin()
16 {
17 InitializeComponent();
18 }
19
20 private void button1_Click(object sender, EventArgs e)
21 {
22 if (!Validator.ValidTextBox(txtUserName, ValidType.Required, lblErrorName))
23 {
24 MessageBox.Show("用户名必填,请输入用户名!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
25 txtUserName.Focus();
26 }
27 else if (!Validator.ValidTextBox(txtPassword, ValidType.Required, lblErrorPassword))
28 {
29 MessageBox.Show("密码必填,请输入用户密码!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
30 txtPassword.Focus();
31 }
32 else
33 {
34 MessageBox.Show("恭喜您,登录成功!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
35 frmMain main = new frmMain();
36 main.Show();
37 this.Hide();
38 }
39 }
40 }
41 }

代码清晰易懂把!希望对大家有启发,这个还可以做成属性,也可以自己扩展textbox控件,各种各样的方法都可以实现,主要要说明的一点,大家多动脑子,多共享点代码,让我们的路走的更宽,更远!

同时,也欢迎各位关注【mrhuo工作室】,网址:http://www.mrhuo.com


 

转载于:https://www.cnblogs.com/MrHuo/archive/2012/03/06/WinformValidator.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值