因为新的项目是一个Winform的项目,其中有大量的数据输入部分,如果要加验证的话比较麻烦,所以自己就动手做了一个输入控件.比较简单.希望对初学者有所帮助.提供代码以供参考.
1
using System.ComponentModel;
2
using System.Text.RegularExpressions;
3
using System.Windows.Forms;
4![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
namespace com.xxxx.Controls
6![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
7![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
8
/// 输入框的处理
9
/// </summary>
10
public class InputEditBox : TextBox
11![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
12
public enum InputType
13![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
14
String, //字符型,
15
Float, //浮点型,
16
Integer, //整型
17
Custom //自定义类型
18
}
19![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
20
public InputType inputType;
21![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
22
public InputEditBox() : base()
23![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
24
}
25![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
26
protected override void OnPaint(PaintEventArgs pe)
27![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
28
// 调用基类 OnPaint
29
base.OnPaint(pe);
30
}
31![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
32![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
重写方法#region 重写方法
33![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
34
private ErrorProvider errorProvider1 = new ErrorProvider();
35
private string customType = string.Empty;
36
private bool isMust = false;
37![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
38
39![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
40
/// 对输入的数据合法检查 在成功验证控件后发生
41
/// </summary>
42
/// <param name="e"></param>
43
protected override void OnValidating(CancelEventArgs e)
44![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
45
base.OnValidating(e);
46
if (CausesValidation)
47![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
48
if (isMust)
49![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
50
if (Text.Trim() == string.Empty)
51![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
52
errorProvider1.SetError(this, "必须输入");
53
e.Cancel = true;
54
return;
55
}
56
}
57
switch (inputType)
58![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
59
case InputType.Integer:
60
if (Text.Trim() != string.Empty)
61![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
62
if (!Regex.IsMatch(Text.Trim(), @"^([1-9]\d*)$|^0$"))
63![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
64
errorProvider1.SetError(this, "请输入整型.");
65
e.Cancel = true;
66
return;
67
}
68
}
69
break;
70
case InputType.Float:
71
if (Text.Trim() != string.Empty)
72![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
73
if (!Regex.IsMatch(Text.Trim(), @"^([+-]?)\\d*\\.?\\d+$"))
74![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
75
errorProvider1.SetError(this, "请输入浮点类型.");
76
e.Cancel = true;
77
return;
78
}
79
}
80
break;
81
case InputType.String:
82
if (Text.Trim() == string.Empty)
83![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
84
errorProvider1.SetError(this, "请输入字符串,不能为空.");
85
e.Cancel = true;
86
return;
87
}
88
break;
89
case InputType.Custom:
90
if (Text.Trim() != string.Empty)
91![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
92
if (!Regex.IsMatch(Text.Trim(), CustomType))
93![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
94
errorProvider1.SetError(this, "请输入指定类型的数值.");
95
e.Cancel = true;
96
return;
97
}
98
}
99
break;
100
}
101
errorProvider1.SetError(this, "");
102
}
103
}
104![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
105
#endregion
106![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
107![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
属性#region 属性
108![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
109
[CategoryAttribute("Data"), DescriptionAttribute("选择类型")]
110
public InputType DataType
111![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
112![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return inputType; }
113![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ inputType = value; }
114
}
115![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
116
[CategoryAttribute("Data"), DescriptionAttribute("自定义验证方式使用的表达式")]
117
public string CustomType
118![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
119![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return customType; }
120![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ customType = value; }
121
}
122![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
123
[CategoryAttribute("Data"), DescriptionAttribute("是否必须输入")]
124
public bool IsMust
125![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
126![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return isMust; }
127![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ isMust = value; }
128
}
129![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
130
#endregion
131
}
132
}
在使用过程中,可能会出现,在点击关闭窗体的"X"时,无法关闭的问题.出现这个问题是由于触发了基类中的Validating事件,所以提供一个笨方法给他家,在窗体的Closing事件中简单设置一下:
e.Cancel = false;
就可以了.
如果大家有什么好的解决方法,也可以提出来,大家一下学习.