C#验证多个文本框中的数据?

C#验证多个文本框中的数据?,c#,.net,validation,textbox,C#,.net,Validation,Textbox,我有一个带有多个文本框的C#表单。在继续之前,我需要验证每个文本框中的输入。如果我对每个文本框的验证规则相同,我是否有办法一次对所有字段应用相同的规则。我想要的输出是一样的。(我想把相关文本框的背景颜色改成粉红色)我的意思是我不想使用类似的东西 validate_txtName(); validate_txtAddress(); validate_txtCity(); 应该有一些标准和简单的方法来做到这一点。。我正在寻求那种方式;) 可能是foreach循环?:) 可能是foreach循环?:

我有一个带有多个文本框的C#表单。在继续之前,我需要验证每个文本框中的输入。如果我对每个文本框的验证规则相同,我是否有办法一次对所有字段应用相同的规则。我想要的输出是一样的。(我想把相关文本框的背景颜色改成粉红色)我的意思是我不想使用类似的东西

validate_txtName();
validate_txtAddress();
validate_txtCity();

应该有一些标准和简单的方法来做到这一点。。我正在寻求那种方式;)

可能是foreach循环?:)

可能是foreach循环?:)

首先,将所有文本框放入列表中。然后在列表上应用ForEach函数,将表示验证规则的lambda表达式作为参数传递

编辑: 我在自己的代码中发现了以下示例:

Core.Model.Settings.Labels.ToList()
.ForEach(x => schedulerStorage1.Appointments.Labels.Add(Color.FromArgb(x.ARGB), x.LabelName));

首先,将所有文本框放入列表中。然后在列表上应用ForEach函数,将表示验证规则的lambda表达式作为参数传递

编辑: 我在自己的代码中发现了以下示例:

Core.Model.Settings.Labels.ToList()
.ForEach(x => schedulerStorage1.Appointments.Labels.Add(Color.FromArgb(x.ARGB), x.LabelName));

我想你可以试试这个。。将所有要验证的控件放在grouper控件中,并使用foreach循环对grouper中的所有控件调用validate。。将所有要验证的控件放在grouper控件中,并使用foreach循环对grouper中的所有控件调用validate,该循环在设计时接受正则表达式字符串以进行验证检查。在执行时,使用一个公共处理程序处理验证事件。下面的代码实现了这一点。您可以删除errorprovider并只使用backcolor逻辑

public class ValidatedTextBox : TextBox
    {
        private IContainer components;
        private Color m_OldBackColor;        

        [Description("Color to be set when validation fails.")]
        public Color BackColorOnFailedValidation
        {
            get
            {
                return m_BackColorOnFailedValidation;
            }

            set
            {
                m_BackColorOnFailedValidation = value;
            }
        }
        private Color m_BackColorOnFailedValidation = Color.Yellow;

        [Description("Message displayed by the error provider.")]
        public string ErrorMessage
        {
            get
            {
                return m_ErrorMessage;
            }

            set
            {
                m_ErrorMessage = value;
            }
        }
        private string m_ErrorMessage = "";


        [Description("Regular expression string to validate the text.")]
        public string RegularExpressionString
        {
            get
            {
                return m_RegularExpressionString;
            }
            set
            {              
                m_RegularExpressionString = value;
            }
        }
        private string m_RegularExpressionString = "";
        private ErrorProvider errorProvider1;

        [Browsable(false)]
        public bool Valid
        {
            get
            {
                return m_Valid;
            }
        }
        private bool m_Valid = true;

        public ValidatedTextBox()
            : base()
        {
            InitializeComponent();
            m_OldBackColor = this.BackColor;
            this.Validating += new System.ComponentModel.CancelEventHandler(ValidatedTextBox_Validating);
            errorProvider1.Clear();
        }

        void ValidatedTextBox_Validating(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (RegularExpressionString != string.Empty)
            {
                Regex regex = new Regex(RegularExpressionString);
                m_Valid = regex.IsMatch(Text);
                SetBackColor();
                if (!Valid)
                {
                    errorProvider1.SetError(this, this.ErrorMessage);
                    this.Focus();
                }
                else
                {
                    errorProvider1.Clear();
                }
            }
        }

        private void SetBackColor()
        {
            if (!Valid)
                BackColor = BackColorOnFailedValidation;
            else
                BackColor = m_OldBackColor;
        }

        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.errorProvider1 = new System.Windows.Forms.ErrorProvider(this.components);
            ((System.ComponentModel.ISupportInitialize)(this.errorProvider1)).BeginInit();
            this.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.errorProvider1)).EndInit();
            this.ResumeLayout(false);

        }
    }

编写自己的控件,该控件在设计时接受正则表达式字符串进行验证检查。在执行时,使用一个公共处理程序处理验证事件。下面的代码实现了这一点。您可以删除errorprovider并只使用backcolor逻辑

public class ValidatedTextBox : TextBox
    {
        private IContainer components;
        private Color m_OldBackColor;        

        [Description("Color to be set when validation fails.")]
        public Color BackColorOnFailedValidation
        {
            get
            {
                return m_BackColorOnFailedValidation;
            }

            set
            {
                m_BackColorOnFailedValidation = value;
            }
        }
        private Color m_BackColorOnFailedValidation = Color.Yellow;

        [Description("Message displayed by the error provider.")]
        public string ErrorMessage
        {
            get
            {
                return m_ErrorMessage;
            }

            set
            {
                m_ErrorMessage = value;
            }
        }
        private string m_ErrorMessage = "";


        [Description("Regular expression string to validate the text.")]
        public string RegularExpressionString
        {
            get
            {
                return m_RegularExpressionString;
            }
            set
            {              
                m_RegularExpressionString = value;
            }
        }
        private string m_RegularExpressionString = "";
        private ErrorProvider errorProvider1;

        [Browsable(false)]
        public bool Valid
        {
            get
            {
                return m_Valid;
            }
        }
        private bool m_Valid = true;

        public ValidatedTextBox()
            : base()
        {
            InitializeComponent();
            m_OldBackColor = this.BackColor;
            this.Validating += new System.ComponentModel.CancelEventHandler(ValidatedTextBox_Validating);
            errorProvider1.Clear();
        }

        void ValidatedTextBox_Validating(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (RegularExpressionString != string.Empty)
            {
                Regex regex = new Regex(RegularExpressionString);
                m_Valid = regex.IsMatch(Text);
                SetBackColor();
                if (!Valid)
                {
                    errorProvider1.SetError(this, this.ErrorMessage);
                    this.Focus();
                }
                else
                {
                    errorProvider1.Clear();
                }
            }
        }

        private void SetBackColor()
        {
            if (!Valid)
                BackColor = BackColorOnFailedValidation;
            else
                BackColor = m_OldBackColor;
        }

        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.errorProvider1 = new System.Windows.Forms.ErrorProvider(this.components);
            ((System.ComponentModel.ISupportInitialize)(this.errorProvider1)).BeginInit();
            this.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.errorProvider1)).EndInit();
            this.ResumeLayout(false);

        }
    }

要使用foreach,我需要把所有的文本框放到一些数据结构中,对吗?要使用foreach,我需要把所有的文本框放到一些数据结构中,对吗?来吧,亨利,你能多花一秒钟写一个比这个更好的例子吗?来吧,亨利,你能多花一秒钟写一个比这个更好的例子吗?