views:

1069

answers:

4

I have a C# form with multiple text boxes. Before proceeding I need to validate the inputs in the each text box. If my validation rule for each text box is same, Do I have any way to apply the same rule for all the fields at once. And my desired output is same. (I want to change the backcolour of the relevant textbox into pink) I mean I don't want to use anything like

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

There should be some standard and easy way to do this.. I am seeking of that way ;)

+1  A: 

maybe foreach loop? :)

x2
To use a foreach I need to put all the text boxes into some data structure right?
Chathuranga Chandrasekara
+1  A: 

First, put all the textboxes in a list. Then apply the ForEach function on the list, passing as argument the lambda expression that represents you're validation rule.

Edit: I've found this example in my own code:

Core.Model.Settings.Labels.ToList()
.ForEach(x => schedulerStorage1.Appointments.Labels.Add(Color.FromArgb(x.ARGB), x.LabelName));
Henri
C'mon Henri, can you please take an extra second to write a better example than that?
Yoely
Whats wrong with that :)
Henri
A: 

you can try this i suppose.. Put all the controls you want to validate in a grouper control and call validate on all the controls inside the grouper using a foreach loop

+1  A: 

Write you own control which accepts a regular expression string for validation check during design time. At execution time handle the Validating event with one common handler. Following code does this. You can remove the errorprovider and just use the backcolor logic.

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);

        }
    }
P.K