Click here to Skip to main content
15,889,034 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hiii,
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
Comments
solanki.net 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

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
C#
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 ...

C#
private void comboBox1_Validating(object sender, CancelEventArgs e)
 {
     if (InvalidYear(sender))
         e.Cancel = true;
 }
 
Share this answer
 
v2
Comments
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900