⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 regexvalidator.cs

📁 csharp课本的源代码
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace RegexValidator
{
    public partial class RegexValidator : UserControl
    {
        //private string errorMessage;
        private string validationExpression;
        //private string text;

        [DefaultValue("RegexValidator1")]
        [Description("与控件关联的文本信息。"),Category("验证")]
        public string RegexText
        {
            get
            {
                return textBoxRegex.Text;
            }
            set
            {
                textBoxRegex.Text = value;
            }
        }


        [DefaultValue("sfd")]
        [Description("校验错误时的文本提示信息。"), Category("验证")]
        public string ErrorMessage
        {
            get
            {
                return labelRegex.Text;
            }
            set
            {
                labelRegex.Text = value;
            }
        }

        [DefaultValue(null)]
        [Description("指定验证时的正则表达式。"), Category("验证")]
        public string ValidationExpression
        {
            get
            {

                return validationExpression;
            }
            set
            {
                try
                {
                    Regex r = new Regex(value);
                    validationExpression = value;
                }
                catch
                {
                    MessageBox.Show(this,"正则表达式语法错误!","错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    validationExpression = null;
                }
            }
        }
        public RegexValidator()
        {
            InitializeComponent();
        }

        private void textBoxRegex_Leave(object sender, EventArgs e)
        {
            labelRegex.Visible = !Regex.IsMatch(textBoxRegex.Text, validationExpression);
            if (labelRegex.Visible)
            {
                MessageBox.Show("请按要求输入", "", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                textBoxRegex.Focus();
            }
            else
            {
                MessageBox.Show("注册验证成功", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -