C#还真是想得周道,
居然可以给控件加上自己想要的属性.
这样,就可以像 检测网页表单那么方便了.
呵呵,可爱的C#
using System;
using System.ComponentModel;
using System.Collections;
using System.Diagnostics;
using System.Windows.Forms;
using System.Text.RegularExpressions;
namespace Supersnake.Components
{
/// <summary>
/// Checker 的摘要说明。
/// </summary>
[ProvideProperty("CheckMsg",typeof(Control))]
[ProvideProperty("CheckType",typeof(Control))]
[ProvideProperty("CheckCanEmpty",typeof(Control))]
[ProvideProperty("CheckMax",typeof(Control))]
[ProvideProperty("CheckMin",typeof(Control))]
[ProvideProperty("CheckReg",typeof(Control))]
public class Checker : Component, IExtenderProvider
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
private Hashtable checkees = new Hashtable();
public Checker(System.ComponentModel.IContainer container)
{
///
/// Windows.Forms 类撰写设计器支持所必需的
///
container.Add(this);
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
public Checker()
{
///
/// Windows.Forms 类撰写设计器支持所必需的
///
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
checkees.Clear();
}
base.Dispose( disposing );
}
组件设计器生成的代码
IExtenderProvider 成员
/// <summary>
/// 检测类型
/// </summary>
public enum CheckTypes
{
None,
Int,
Float,
Email,
Telephone,
Handset,
QQ,
Numbers,
Range,
Length,
Regex
}
/// <summary>
/// 内部类 CheckInfo, 用来存储检测信息
/// </summary>
internal class CheckInfo
{
public CheckTypes CheckType = CheckTypes.None;
public string CheckMsg = null;
public bool CheckCanEmpty = true;
public int CheckMin = 0;
public int CheckMax = 0;
public string CheckReg = null;
}
/// <summary>
/// [CheckType]
/// </summary>
/// <param name="control"></param>
/// <returns></returns>
[
DefaultValue(CheckTypes.None),
Description("检测类型")/*,
RefreshProperties(RefreshProperties.All)*/
]
public CheckTypes GetCheckType(Control control)
{
CheckTypes type;
if(checkees.Contains(control))
{
type = ((CheckInfo)checkees[control]).CheckType;
}
else
{
type = CheckTypes.None;
}
return type;
}
public void SetCheckType(Control control,CheckTypes value)
{
CheckInfo info;
if(checkees.Contains(control))
{
info = (CheckInfo)checkees[control];
}
else
{
info = new CheckInfo();
}
info.CheckType = value;
checkees[control] = info;
}
/// <summary>
/// [CheckMsg]
/// </summary>
/// <param name="control"></param>
/// <returns></returns>
[
DefaultValue(null),
Description("未通过检测时的信息")
]
public string GetCheckMsg(Control control)
{
string msg;
if(checkees.Contains(control))
{
msg = ((CheckInfo)checkees[control]).CheckMsg;
}
else
{
msg = null;
}
return msg;
}
public void SetCheckMsg(Control control,string value)
{
CheckInfo info;
if(checkees.Contains(control))
{
info = (CheckInfo)checkees[control];
}
else
{
info = new CheckInfo();
}
info.CheckMsg = value;
checkees[control] = info;
}
/// <summary>
/// [CheckCanEmpty]
/// </summary>
/// <param name="control"></param>
/// <returns></returns>
[
DefaultValue(true),
Description("是否可为空")
]
public bool GetCheckCanEmpty(Control control)
{
bool canempty;
if(checkees.Contains(control))
{
canempty = ((CheckInfo)checkees[control]).CheckCanEmpty;
}
else
{
canempty = true;
}
return canempty;
}
public void SetCheckCanEmpty(Control control,bool value)
{
CheckInfo info;
if(checkees.Contains(control))
{
info = (CheckInfo)checkees[control];
}
else
{
info = new CheckInfo();
}
info.CheckCanEmpty = value;
checkees[control] = info;
}
/// <summary>
/// [CheckMax]
/// </summary>
/// <param name="control"></param>
/// <returns></returns>
[
DefaultValue(0),
Description("最小值")
]
public int GetCheckMax(Control control)
{
int max;
if(checkees.Contains(control))
{
max = ((CheckInfo)checkees[control]).CheckMax;
}
else
{
max = 0;
}
return max;
}
public void SetCheckMax(Control control,int value)
{
CheckInfo info;
if(checkees.Contains(control))
{
info = (CheckInfo)checkees[control];
}
else
{
info = new CheckInfo();
}
info.CheckMax = value;
checkees[control] = info;
}
/// <summary>
/// [CheckReg]
/// </summary>
/// <param name="control"></param>
/// <returns></returns>
[
DefaultValue(null),
Description("正则表达式")
]
public string GetCheckReg(Control control)
{
string reg;
if(checkees.Contains(control))
{
reg = ((CheckInfo)checkees[control]).CheckReg;
}
else
{
reg = null;
}
return reg;
}
public void SetCheckReg(Control control,string value)
{
CheckInfo info;
if(checkees.Contains(control))
{
info = (CheckInfo)checkees[control];
}
else
{
info = new CheckInfo();
}
info.CheckReg = value;
checkees[control] = info;
}
/// <summary>
/// [CheckMin]
/// </summary>
/// <param name="control"></param>
/// <returns></returns>
[
DefaultValue(0),
Description("最大值")
]
public int GetCheckMin(Control control)
{
int min;
if(checkees.Contains(control))
{
min = ((CheckInfo)checkees[control]).CheckMin;
}
else
{
min = 0;
}
return min;
}
public void SetCheckMin(Control control,int value)
{
CheckInfo info;
if(checkees.Contains(control))
{
info = (CheckInfo)checkees[control];
}
else
{
info = new CheckInfo();
}
info.CheckMin = value;
checkees[control] = info;
}
/// <summary>
/// 检测
/// </summary>
/// <returns>
/// True: 检测通过 False: 检测不通过
/// </returns>
public bool Check()
{
bool passcheck = true;
CheckInfo info;
Regex re = null;
string msg = null;
string sre = null;
int len = 0;
string errmsg = null;
foreach(Control control in checkees.Keys)
{
info = (CheckInfo)checkees[control];
msg = info.CheckMsg;
sre = null;
len = 0;
if(info.CheckCanEmpty && control.Text.Length==0)
passcheck=true;
else
switch(info.CheckType)
{
case CheckTypes.None:
break;
case CheckTypes.Int:
sre = "(^0$)|(^[1-9]\\d+$)";
break;
case CheckTypes.Float:
sre = "^\\d+\\.?\\d*$";
break;
case CheckTypes.Telephone:
sre = "^\\d+-?\\d+$";
break;
case CheckTypes.Handset:
sre = "^\\d+-?\\d+$";
break;
case CheckTypes.QQ:
sre = "^\\d{5,20}$";
break;
case CheckTypes.Numbers:
sre = "^\\d+$";
break;
case CheckTypes.Email:
sre = "^\\w+@\\w+\\.\\w+$";
break;
case CheckTypes.Length:
len = control.Text.Length;
break;
case CheckTypes.Range:
try
{
len = (int)decimal.Parse(control.Text);
}
catch(System.FormatException e)
{
errmsg = e.Message;
passcheck = false;
}
break;
case CheckTypes.Regex:
sre = info.CheckReg;
break;
default:
break;
}
if(len > info.CheckMax || len < info.CheckMin)
passcheck = false;
if(sre!=null)
{
re = new Regex(sre);
if(!re.IsMatch(control.Text))
passcheck = false;
}
if(!passcheck)
{
control.Select();
MessageBox.Show(errmsg + "\n" + info.CheckMsg);
return false; // not pass check
}
}//~foreach
return passcheck; // passed check
}
}
}
using System.ComponentModel;
using System.Collections;
using System.Diagnostics;
using System.Windows.Forms;
using System.Text.RegularExpressions;
namespace Supersnake.Components
{
/// <summary>
/// Checker 的摘要说明。
/// </summary>
[ProvideProperty("CheckMsg",typeof(Control))]
[ProvideProperty("CheckType",typeof(Control))]
[ProvideProperty("CheckCanEmpty",typeof(Control))]
[ProvideProperty("CheckMax",typeof(Control))]
[ProvideProperty("CheckMin",typeof(Control))]
[ProvideProperty("CheckReg",typeof(Control))]
public class Checker : Component, IExtenderProvider
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
private Hashtable checkees = new Hashtable();
public Checker(System.ComponentModel.IContainer container)
{
///
/// Windows.Forms 类撰写设计器支持所必需的
///
container.Add(this);
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
public Checker()
{
///
/// Windows.Forms 类撰写设计器支持所必需的
///
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
checkees.Clear();
}
base.Dispose( disposing );
}
组件设计器生成的代码
IExtenderProvider 成员
/// <summary>
/// 检测类型
/// </summary>
public enum CheckTypes
{
None,
Int,
Float,
Email,
Telephone,
Handset,
QQ,
Numbers,
Range,
Length,
Regex
}
/// <summary>
/// 内部类 CheckInfo, 用来存储检测信息
/// </summary>
internal class CheckInfo
{
public CheckTypes CheckType = CheckTypes.None;
public string CheckMsg = null;
public bool CheckCanEmpty = true;
public int CheckMin = 0;
public int CheckMax = 0;
public string CheckReg = null;
}
/// <summary>
/// [CheckType]
/// </summary>
/// <param name="control"></param>
/// <returns></returns>
[
DefaultValue(CheckTypes.None),
Description("检测类型")/*,
RefreshProperties(RefreshProperties.All)*/
]
public CheckTypes GetCheckType(Control control)
{
CheckTypes type;
if(checkees.Contains(control))
{
type = ((CheckInfo)checkees[control]).CheckType;
}
else
{
type = CheckTypes.None;
}
return type;
}
public void SetCheckType(Control control,CheckTypes value)
{
CheckInfo info;
if(checkees.Contains(control))
{
info = (CheckInfo)checkees[control];
}
else
{
info = new CheckInfo();
}
info.CheckType = value;
checkees[control] = info;
}
/// <summary>
/// [CheckMsg]
/// </summary>
/// <param name="control"></param>
/// <returns></returns>
[
DefaultValue(null),
Description("未通过检测时的信息")
]
public string GetCheckMsg(Control control)
{
string msg;
if(checkees.Contains(control))
{
msg = ((CheckInfo)checkees[control]).CheckMsg;
}
else
{
msg = null;
}
return msg;
}
public void SetCheckMsg(Control control,string value)
{
CheckInfo info;
if(checkees.Contains(control))
{
info = (CheckInfo)checkees[control];
}
else
{
info = new CheckInfo();
}
info.CheckMsg = value;
checkees[control] = info;
}
/// <summary>
/// [CheckCanEmpty]
/// </summary>
/// <param name="control"></param>
/// <returns></returns>
[
DefaultValue(true),
Description("是否可为空")
]
public bool GetCheckCanEmpty(Control control)
{
bool canempty;
if(checkees.Contains(control))
{
canempty = ((CheckInfo)checkees[control]).CheckCanEmpty;
}
else
{
canempty = true;
}
return canempty;
}
public void SetCheckCanEmpty(Control control,bool value)
{
CheckInfo info;
if(checkees.Contains(control))
{
info = (CheckInfo)checkees[control];
}
else
{
info = new CheckInfo();
}
info.CheckCanEmpty = value;
checkees[control] = info;
}
/// <summary>
/// [CheckMax]
/// </summary>
/// <param name="control"></param>
/// <returns></returns>
[
DefaultValue(0),
Description("最小值")
]
public int GetCheckMax(Control control)
{
int max;
if(checkees.Contains(control))
{
max = ((CheckInfo)checkees[control]).CheckMax;
}
else
{
max = 0;
}
return max;
}
public void SetCheckMax(Control control,int value)
{
CheckInfo info;
if(checkees.Contains(control))
{
info = (CheckInfo)checkees[control];
}
else
{
info = new CheckInfo();
}
info.CheckMax = value;
checkees[control] = info;
}
/// <summary>
/// [CheckReg]
/// </summary>
/// <param name="control"></param>
/// <returns></returns>
[
DefaultValue(null),
Description("正则表达式")
]
public string GetCheckReg(Control control)
{
string reg;
if(checkees.Contains(control))
{
reg = ((CheckInfo)checkees[control]).CheckReg;
}
else
{
reg = null;
}
return reg;
}
public void SetCheckReg(Control control,string value)
{
CheckInfo info;
if(checkees.Contains(control))
{
info = (CheckInfo)checkees[control];
}
else
{
info = new CheckInfo();
}
info.CheckReg = value;
checkees[control] = info;
}
/// <summary>
/// [CheckMin]
/// </summary>
/// <param name="control"></param>
/// <returns></returns>
[
DefaultValue(0),
Description("最大值")
]
public int GetCheckMin(Control control)
{
int min;
if(checkees.Contains(control))
{
min = ((CheckInfo)checkees[control]).CheckMin;
}
else
{
min = 0;
}
return min;
}
public void SetCheckMin(Control control,int value)
{
CheckInfo info;
if(checkees.Contains(control))
{
info = (CheckInfo)checkees[control];
}
else
{
info = new CheckInfo();
}
info.CheckMin = value;
checkees[control] = info;
}
/// <summary>
/// 检测
/// </summary>
/// <returns>
/// True: 检测通过 False: 检测不通过
/// </returns>
public bool Check()
{
bool passcheck = true;
CheckInfo info;
Regex re = null;
string msg = null;
string sre = null;
int len = 0;
string errmsg = null;
foreach(Control control in checkees.Keys)
{
info = (CheckInfo)checkees[control];
msg = info.CheckMsg;
sre = null;
len = 0;
if(info.CheckCanEmpty && control.Text.Length==0)
passcheck=true;
else
switch(info.CheckType)
{
case CheckTypes.None:
break;
case CheckTypes.Int:
sre = "(^0$)|(^[1-9]\\d+$)";
break;
case CheckTypes.Float:
sre = "^\\d+\\.?\\d*$";
break;
case CheckTypes.Telephone:
sre = "^\\d+-?\\d+$";
break;
case CheckTypes.Handset:
sre = "^\\d+-?\\d+$";
break;
case CheckTypes.QQ:
sre = "^\\d{5,20}$";
break;
case CheckTypes.Numbers:
sre = "^\\d+$";
break;
case CheckTypes.Email:
sre = "^\\w+@\\w+\\.\\w+$";
break;
case CheckTypes.Length:
len = control.Text.Length;
break;
case CheckTypes.Range:
try
{
len = (int)decimal.Parse(control.Text);
}
catch(System.FormatException e)
{
errmsg = e.Message;
passcheck = false;
}
break;
case CheckTypes.Regex:
sre = info.CheckReg;
break;
default:
break;
}
if(len > info.CheckMax || len < info.CheckMin)
passcheck = false;
if(sre!=null)
{
re = new Regex(sre);
if(!re.IsMatch(control.Text))
passcheck = false;
}
if(!passcheck)
{
control.Select();
MessageBox.Show(errmsg + "\n" + info.CheckMsg);
return false; // not pass check
}
}//~foreach
return passcheck; // passed check
}
}
}