jeudi 10 septembre 2015

Display an alert message if a list of check box is uncheck C#

I have a list of checkbox that stored in an array. Let say I have 6 check boxes, and If none of the checkboxes are checked, than it will display an message in the message box saying "empty field is not allowed. I have an idea how to do that but it didn't seem right. Help will be appreciated

Here is my code.

   private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox[] mySecondaryCheckBoxArray = new CheckBox[6];

            mySecondaryCheckBoxArray[0] = secondaryCheckBox1;
            mySecondaryCheckBoxArray[1] = secondaryCheckBox2;
            mySecondaryCheckBoxArray[2] = secondaryCheckBox3;
            mySecondaryCheckBoxArray[3] = secondaryCheckBox4;
            mySecondaryCheckBoxArray[4] = secondaryCheckBox5;
            mySecondaryCheckBoxArray[5] = secondaryCheckBox6;


                //if check box "ALL" is checked, disable the other check box
                foreach(CheckBox secondaryCB in mySecondaryCheckBoxArray)
                {
                    if(secondaryCheckBoxALL.Checked== true)
                    {
                        secondaryCB.Enabled = false;
                    }else
                    {
                        secondaryCB.Enabled = true;
                    }

                }


        }

    //button name call button1
     private void button_Click(object sender, EventArgs e)
        {
         //When this button is clicked, it checked if all the checkbox is checked. If not, display an message in the message box

          CheckBox[] myConfirmSecondaryCheckBoxArray = new CheckBox[6];

            myConfirmSecondaryCheckBoxArray[0] = classCheckBoxALL;
            myConfirmSecondaryCheckBoxArray[1] = classCheckBoxA;
            myConfirmSecondaryCheckBoxArray[2] = classCheckBoxB;
            myConfirmSecondaryCheckBoxArray[3] = classCheckBoxC;
            myConfirmSecondaryCheckBoxArray[4] = classCheckBoxD;
            myConfirmSecondaryCheckBoxArray[5] = classCheckBoxE;

           //Check for loop
           foreach (CheckBox secondaryCB in myConfirmSecondaryCheckBoxArray)
            {   
                //This part doesn't work because the message box showed up 6                  times
               if (secondaryCB.Check ==false)
                {
                    MessageBox.Show("Empty field not allowed");
                }
            }



       }




Aucun commentaire:

Enregistrer un commentaire