textBOX验证控件,自动验证数据正确与否!只需选择你的数据类型

    技术2022-05-11  70

    using  System; using  System.Collections.Generic; using  System.ComponentModel; using  System.Drawing; using  System.Data; using  System.Text; using  System.Windows.Forms; namespace  Tigers_WindowsControlLibrary.Windows {    public partial class TextBox_Validate : Tigers_WindowsControlLibrary.Windows.TextBox    {        public TextBox_Validate()        {            InitializeComponent();            this.TextChanged += new EventHandler(TextBox_Validate_TextChanged);            this.Leave += new EventHandler(TextBox_Validate_Leave);        }        //焦点移动开时错误数据必须修改        void TextBox_Validate_Leave(object sender, EventArgs e)        {            if (!this._checked)            {                this.Focus();            }            //throw new Exception("The method or operation is not implemented.");        }        //验证用户的输入        void TextBox_Validate_TextChanged(object sender, EventArgs e)        {            //当用户没有数据数据,并且数据允许为空时            if (this.Text.Equals(string.Empty) && this._dataIsNull == true)            {                this._checked = true;            }            else            {                this._checked = Tigers_WindowsControlLibrary.DataValidate.RegularValidate.FitString(this._validateString, this.Text);            }            //throw new Exception("The method or operation is not implemented.");        }        private bool _dataIsNull = true;        [        Category("数据验证"),        Description("数据是否允许为空"),        DefaultValue(true)        ]        public bool DataIsNull        {            get { return _dataIsNull; }            set { _dataIsNull = value; }        }        //数据是否通过验证        private bool _checked = false;        [        Category("数据验证"),        Description("数据验证是否成功"),        DefaultValue(false)        ]        public bool Checked        {            get            {                if (this.Text.Equals(string.Empty) && this._dataIsNull == true)                {                    return true;                }                else                {                    return _checked;                }            }           // set { _checked = value; }        }        //验证数据的方式        private string _validateString = string.Empty;        [        Editor(typeof(TextBox_Validate_ListBox_DropDownEdit), typeof(System.Drawing.Design.UITypeEditor)),        Category("数据验证"),        Description("对数据验证的正则表达式"),        DefaultValue("")        ]        public string ValidateString        {            get { return _validateString; }            set { _validateString = value; }        }    }}

     

     

    using  System; using  System.Collections.Generic; using  System.Text; namespace  Tigers_WindowsControlLibrary.Windows {    class TextBox_Validate_ListBox_DropDownEdit : System.Drawing.Design.UITypeEditor    {        public override System.Drawing.Design.UITypeEditorEditStyle GetEditStyle(System.ComponentModel.ITypeDescriptorContext context)        {            if (context != null && context.Instance != null)            {                return System.Drawing.Design.UITypeEditorEditStyle.DropDown;            }            return base.GetEditStyle(context);        }        public override object EditValue(System.ComponentModel.ITypeDescriptorContext context, IServiceProvider provider, object value)        {            System.Windows.Forms.Design.IWindowsFormsEditorService editorService = null;            if (context != null && context.Instance != null && provider != null)            {                editorService = (System.Windows.Forms.Design.IWindowsFormsEditorService)provider.GetService(typeof(System.Windows.Forms.Design.IWindowsFormsEditorService));                if (editorService != null)                {                    TextBox_Validate textBox_Validate = (TextBox_Validate)context.Instance;                    Tigers_WindowsControlLibrary.PulicControl.TextBox_Validate_ListBox textBox_Validate_ListBox = new Tigers_WindowsControlLibrary.PulicControl.TextBox_Validate_ListBox(textBox_Validate.ValidateString);                    editorService.DropDownControl(textBox_Validate_ListBox);                    value = textBox_Validate_ListBox.ValidateString;                    return value;                }            }            return value;        }    }}

     

     

     

     

    using  System; using  System.Collections.Generic; using  System.ComponentModel; using  System.Drawing; using  System.Data; using  System.Text; using  System.Windows.Forms; namespace  Tigers_WindowsControlLibrary.PulicControl {     partial class TextBox_Validate_ListBox : System.Windows.Forms.UserControl    {                public TextBox_Validate_ListBox()        {            InitializeComponent();        }        private string _validateString = string.Empty;        public string ValidateString        {            get { return _validateString; }            set { _validateString = value; }        }        public TextBox_Validate_ListBox(string validateString)        {            InitializeComponent();            this._validateString = "validateString";            switch (validateString)            {                case @"w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*":this.ListBox_Select.SelectedIndex = 0; break;                case @"[a-zA-z]+://[^s]*": this.ListBox_Select.SelectedIndex = 1; break;                case @"d+.d+.d+.d+": this.ListBox_Select.SelectedIndex = 2; break;                case @"[1-9][0-9]{4,}": this.ListBox_Select.SelectedIndex = 3; break;                case @"[1-9]d{5}(?!d)": this.ListBox_Select.SelectedIndex = 4; break;                case @"^((d{3,4}-)|d{3.4}-)?d{7,8}$": this.ListBox_Select.SelectedIndex = 5; break;                case @"d{15}|d{18}": this.ListBox_Select.SelectedIndex = 6; break;                case @"[一-龥]": this.ListBox_Select.SelectedIndex = 7; break;                //以字母开头,长度在6~18之间,只能包含字符、数字和下划线。                case @"^[a-zA-Z]w{5,17}$": this.ListBox_Select.SelectedIndex = 8; break;                case @"^[a-zA-Z][a-zA-Z0-9_]{4,15}$": this.ListBox_Select.SelectedIndex = 9; break;                case @"<(S*?)[^>]*>.*?</>|<.*? />": this.ListBox_Select.SelectedIndex = 10; break;                case @"^-?[1-9]d*$": this.ListBox_Select.SelectedIndex = 11; break;                case @"^[1-9]d*$": this.ListBox_Select.SelectedIndex = 12; break;                case @"^-[1-9]d*$ ": this.ListBox_Select.SelectedIndex = 13; break;                case @"^[1-9]d*|0$": this.ListBox_Select.SelectedIndex = 14; break;                case @"^-[1-9]d*|0$": this.ListBox_Select.SelectedIndex = 15; break;                case @"^-?([1-9]d*.d*|0.d*[1-9]d*|0?.0+|0)$": this.ListBox_Select.SelectedIndex = 16; break;                case @"^[1-9]d*.d*|0.d*[1-9]d*$": this.ListBox_Select.SelectedIndex = 17; break;                case @"^-([1-9]d*.d*|0.d*[1-9]d*)$": this.ListBox_Select.SelectedIndex = 18; break;                case @"^[1-9]d*.d*|0.d*[1-9]d*|0?.0+|0$": this.ListBox_Select.SelectedIndex = 19; break;                case @"^(-([1-9]d*.d*|0.d*[1-9]d*))|0?.0+|0$": this.ListBox_Select.SelectedIndex = 20; break;                case @"^[A-Za-z]+$": this.ListBox_Select.SelectedIndex = 21; break;                case @"^[A-Z]+$": this.ListBox_Select.SelectedIndex = 22; break;                case @"^[a-z]+$": this.ListBox_Select.SelectedIndex = 23; break;                case @"^[A-Za-z0-9]+$": this.ListBox_Select.SelectedIndex = 24; break;                case @"^w+$": this.ListBox_Select.SelectedIndex = 25; break;                default: this.ListBox_Select.SelectedIndex = 26; break;            }                   }        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)        {            switch (this.ListBox_Select.SelectedItem.ToString().Trim())            {                case "E-mail----------": this._validateString = @"w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*"; break;                case "URL-Http": this._validateString = @"[a-zA-z]+://[^s]*"; break;                case "IP地址": this._validateString = @"d+.d+.d+.d+"; break;                case "QQ": this._validateString = @"[1-9][0-9]{4,}"; break;                case "邮编": this._validateString = @"[1-9]d{5}(?!d)"; break;                case "电话号": this._validateString = @"^((d{3,4}-)|d{3.4}-)?d{7,8}$"; break;                case "身份证": this._validateString = @"d{15}|d{18}"; break;                case "中文": this._validateString = @"[一-龥]"; break;                                //以字母开头,长度在6~18之间,只能包含字符、数字和下划线。                case "密码": this._validateString = @"^[a-zA-Z]w{5,17}$"; break;                case "帐号": this._validateString = @"^[a-zA-Z][a-zA-Z0-9_]{4,15}$"; break;                case "HTML标记": this._validateString = @"<(S*?)[^>]*>.*?</>|<.*? />"; break;                case "整型-----------": this._validateString = @"^-?[1-9]d*$"; break;                case "正整数": this._validateString = @"^[1-9]d*$"; break;                case "负整数": this._validateString = @"^-[1-9]d*$ "; break;                case "非负整数": this._validateString = @"^[1-9]d*|0$"; break;                case "非正整数": this._validateString = @"^-[1-9]d*|0$"; break;                case "浮点数--------": this._validateString = @"^-?([1-9]d*.d*|0.d*[1-9]d*|0?.0+|0)$"; break;                case "正浮点数": this._validateString = @"^[1-9]d*.d*|0.d*[1-9]d*$"; break;                case "负浮点数": this._validateString = @"^-([1-9]d*.d*|0.d*[1-9]d*)$"; break;                case "非负浮点数": this._validateString = @"^[1-9]d*.d*|0.d*[1-9]d*|0?.0+|0$"; break;                case "非正浮点数": this._validateString = @"^(-([1-9]d*.d*|0.d*[1-9]d*))|0?.0+|0$"; break;                case "英文--------------": this._validateString = @"^[A-Za-z]+$"; break;                case "英文(大写)": this._validateString = @"^[A-Z]+$"; break;                case "英文(小写)": this._validateString = @"^[a-z]+$"; break;                case "数字和英文": this._validateString = @"^[A-Za-z0-9]+$"; break;                case "数字和英文或下划线": this._validateString = @"^w+$"; break;                                          }            this.ProcessDialogKey(Keys.Enter);            //;                   }        private void Bt_Close_Click(object sender, EventArgs e)        {            this.ProcessDialogKey(Keys.Enter);           // this.Dispose();        }    }}

     

     

     

     


    最新回复(0)