Click here to Skip to main content
13,005,198 members (61,258 online)
Rate this:
Please Sign up or sign in to vote.
See more:
I have 4 comboBoxes each filled with years.I want to validate such that value in comboBox1 must be greater than other 3,then value in comboBox2 must be greater than next two comboBoxes and so on.Plz help me out....
Posted 11-Mar-13 19:02pm
Comments 12-Mar-13 1:33am
where are your using combobox inside grid or where ? and on which event u want to validate ? explain your question
Dev Gupta from Mumbai,India 12-Mar-13 2:56am
I'm using comboBox inside the panel.I want to validate it on validating event's of each comboBox...

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Firstly I personally dislike cross-validation between controls on the validating events of each of the controls - it's a little bit too easy to get into a right mess where you have to enter valid data before you can even exit the form - be careful of this (My personal preference is cross-validation on a specific event on say a button push - "Ok" or "Submit")

Having said that ... something like this should work...

A generic validation function
 private bool InvalidYear(object sender)
            // cast the sender object to a ComboBox so that we can access it's properties
            ComboBox cb = (ComboBox)sender;
            /* Find out the number of this combo box  - Note there are loads of ways of doing this but
             * I've assumed they're called comboBox1, comboBox2, comboBox3, comboBox4
             * and I'm just removing the text "combBox" from the name 
             * I'm also not assuming that their tabindexes are consecutive 
             * and remember the user can select them in any order
            int cbNum = int.Parse(cb.Name.Replace("comboBox", ""));
            // Locate the comboBoxes before and after the one we're validating
            // Taking care at either end of the list of comboBoxes...
            ComboBox prev = null, post = null;
            if (cbNum > 1)
                prev = (ComboBox)this.Controls.Find("comboBox" + (cbNum - 1).ToString(), true)[0];
            if (cbNum < 4)
                post = (ComboBox)this.Controls.Find("comboBox" + (cbNum + 1).ToString(), true)[0];
            bool errorFound = false;
            // don't validate against a checkbox the user hasn't picked yet!
            if (prev != null && prev.Text != "")
                if (cb.Text.CompareTo(prev.Text) <= 0)  // this one must be greater than previous
                    errorFound = true;
            if (post != null && post.Text != "")
                if (cb.Text.CompareTo(post.Text) >= 0) // this one must be less than the one after
                    errorFound = true;

            if (errorFound) MessageBox.Show("Invalid year");
            return errorFound;

And then you can call this from each of the comboBoxes like this ...

private void comboBox1_Validating(object sender, CancelEventArgs e)
     if (InvalidYear(sender))
         e.Cancel = true;
Menon Santosh 12-Mar-13 8:53am
Nice Solution
CHill60 12-Mar-13 9:00am
Thank you!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170627.1 | Last Updated 12 Mar 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100