使用正则表达式进行数据有效性校验的TextBox控件

从TextBox集成,加入正则表达式校验数据的功能,应该有一定用处,我在CSDN的共享软件里面提交上去了,有兴趣的哥们可以去看看。这里粘上源代码,最起码让我这样的低手看看,^_^

file://RegexTextBox.CS

/**

 * 创建于 2004 217

 * 任何人都可以免费使用本控件,以及免费获得此控件的源代码

 * 有关正则表达式内容请查阅 MSDN 或相关书籍

 */

 

//导入程序使用的.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);

                     return IsVerifySuccess;

              }

             

              //事件处理函数:校验数据

              private void OnValidatingData(object sender, System.ComponentModel.CancelEventArgs e)

              {

                     if(!ToVerify)//不需要校验直接退出

                     {

                            e.Cancel = false;

                            return;

                     }

                     if(!VerifyData())//校验不正确..

                     {

                            if(this.IsDisplayError)//要求显示校验错误提示..

                            {

                                   MessageBox.Show(VerifyErrorString,"警告:");

                            }

                            else//要求不显示校验错误提示..

                            {

                                   this.SelectAll();

                            }//end if

                            e.Cancel = true;

                     }

                     else//校验正确..

                     {

                            e.Cancel = false;

                     }//end if

              }

       }//End Class

}//End NameSpace

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页