前段时间练手做了一个小小TextBox控件,用于校验用户的输入。简单把创建过程写一写,对于新手或许有点用。
首先新建一个windows控件库项目,名称定为MyTextBox。
打开解决方案资源管理器,右键点击UserControl1.cs文件->查看代码。
修改类的名称为CusTextBox),并修改其继承的父类为System.Windows.Forms.TextBox。这样就拥有了一般TextBox的特性。
为CusTextBox类添加所需要的字段和属性:
private verifyMod _verifyMod;
public verifyMod VerifyMod
{
get {return _verifyMod;}
set { _verifyMod = value;}
}
verifyMod是要提供的校验功能属性,在其他应用程序使用这个控件时,可以在属性窗口中选择所需要的特定功能属性。其中NOR是不作校验(此时相当于一般控件), NUM是作数字校验(0-10000), CHAR是作字母校验(全是字母且不多于10位), AIR是作航班校验(前两位必须为大写字母,后四位必须是数字)。
然后加入各功能的校验函数VerifyNum()、VerifyChar()、VerifyAir(),在函数中使用正则表达式来作格式校验(当然也能用其他方法)。
覆写控件的OnLeave事件处理函数,使得当输入焦点离开控件时开始校验。
所有代码如下:
2 using System.Collections;
3 using System.ComponentModel;
4 using System.Drawing;
5 using System.Data;
6 using System.Windows.Forms;
7 using System.Text.RegularExpressions;
8
9 namespace MyTextBox
10 {
11 /// <summary>
12 /// 自定义的TextBox控件,可对输入进行数字和字母的校验
13 /// </summary>
14
15 public class CusTextBox : System.Windows.Forms.TextBox
16 {
17 /// <summary>
18 /// 必需的设计器变量。
19 /// </summary>
20 private System.ComponentModel.Container components = null;
21
22 /// <summary>
23 /// 自定义的校验模式属性,_verifyMod为NOR, NUM, CHAR, AIR时分别表示
24 /// “不作校验”、“作整数校验”、“作字母校验”和“作航班校验”
25 /// </summary>
26 public enum verifyMod{NOR, NUM, CHAR, AIR};
27 private verifyMod _verifyMod;
28 public verifyMod VerifyMod
29 {
30 get {return _verifyMod;}
31 set { _verifyMod = value;}
32 }
33
34 public CusTextBox()
35 {
36 InitializeComponent();
37
38 //初始时将属性设为不做校验
39 _verifyMod = verifyMod.NOR;
40 }
41
42 /// <summary>
43 /// 校验函数
44 /// <summary>
45 private void VerifyNum()
46 {
47 string str = this.Text;
48 string regexStr = @"[0-9]{5}";
49
50 Regex r = new Regex(regexStr, RegexOptions.IgnorePatternWhitespace);
51 MatchCollection m = r.Matches(str);
52 if (m.Count != 1 || str.Length > 5)
53 {
54 MessageBox.Show("请输入0-10000之间的整数");
55 }
56 }
57
58 private void VerifyChar()
59 {
60 string str = this.Text;
61 string regexStr = @"([^A-Z]|[^a-z])";
62
63 Regex r = new Regex(regexStr, RegexOptions.IgnorePatternWhitespace);
64 Match m = r.Match(str);
65
66 if (m.Success || str.Length > 10)
67 {
68 MessageBox.Show("输入的内容必须为字母且不超过10个字符");
69 }
70 }
71
72 private void VerifyAir()
73 {
74 string str = this.Text;
75 string regexStr = @"[A-Z]{2}[0-9]{4}";
76
77 Regex r = new Regex(regexStr, RegexOptions.IgnorePatternWhitespace);
78 Match m = r.Match(str);
79
80 if (!m.Success || str.Length > 6)
81 {
82 MessageBox.Show("航班号错误!");
83 }
84 }
85
86 /// <summary>
87 /// 重写TextBox方法,在输入焦点离开时调用,此时开始校验
88 /// <summary>
89 protected override void OnLeave(EventArgs e)
90 {
91 switch (_verifyMod)
92 {
93 case verifyMod.NUM: VerifyNum();break;
94 case verifyMod.CHAR: VerifyChar();break;
95 case verifyMod.AIR: VerifyAir();break;
96 default: break;
97 }
98
99 base.OnLeave (e);
100 }
101
102
103 /// <summary>
104 /// 清理所有正在使用的资源。
105 /// </summary>
106 protected override void Dispose( bool disposing )
107 {
108 if( disposing )
109 {
110 if( components != null )
111 components.Dispose();
112 }
113 base.Dispose( disposing );
114 }
115
116 #region 组件设计器生成的代码
117 /// <summary>
118 /// 设计器支持所需的方法 - 不要使用代码编辑器
119 /// 修改此方法的内容。
120 /// </summary>
121 private void InitializeComponent()
122 {
123
124 }
125 #endregion
126 }
127 }
128
最后编译成一个dll文件,在项目的bin\debug文件夹中可以找到。
在使用时,必须先在应用程序中添加这个dll文件,方法是:打开解决方案资源管理器,右键单击引用->添加引用->浏览,然后找到dll文件所在的文件夹并选择这个dll文件。在组件列表中可以看到这个dll文件已经添加进来,然后选择它并点击选择按钮将其导入到项目中。最后就可以在“工具箱”->“我的用户控件”中找到这个控件,并且像使用其他控件一样使用它。