日期:2012-03-18  浏览次数:20550 次

//导入程序使用的.net类库

using System;

using System.Drawing;

using System.Windows.Forms;

using System.Text.RegularExpressions;



//命名空间RegexEditor,以后将继续扩充....

namespace RegexEditor

{

/** RegexTextBox继承自TextBox控件

* 能够通过正则表达式对数据进行有效性校验

*/

public class RegexTextBox : System.Windows.Forms.TextBox

{

private string strRegex;//用于校验的正则表达式

private string strVerifyError;//校验错误的提示

private bool bIsVerifySuccess;//是否检验成功

private bool bIsDisplayError;//是否显示错误

private bool bToVerify;//是否进行校验

//属性:用于校验的正则表达式

public string RegexString

{

get

{

return strRegex;

}

set

{

strRegex = value;

}

}

//属性:检验错误的提示

public string VerifyErrorString

{

get

{

return strVerifyError;

}

set

{

strVerifyError = value;

}

}

//属性:是否检验成功

public bool IsVerifySuccess

{

get

{

return bIsVerifySuccess;

}

set

{

bIsVerifySuccess = value;

}

}

//属性:是否显示校验错误

public bool IsDisplayError

{

get

{

return bIsDisplayError;

}

set

{

bIsDisplayError = value;

}

}

public bool ToVerify

{

get

{

return bToVerify;

}

set

{

bToVerify = value;

}

}

//构造函数

public RegexTextBox()

{

InitializeComponent();

}



//初始化变量以及控件属性

void InitializeComponent()

{

RegexString = "";

VerifyErrorString = "";

IsVerifySuccess = true;

IsDisplayError = false;

ToVerify = false;

this.Name = "RegexTextBox";

this.Validating += new System.ComponentModel.CancelEventHandler(this.OnValidatingData);

}



//私有过程:校验数据

private bool VerifyData()

{

IsVerifySuccess = Regex.IsMatch(this.Text,RegexString);