dimanche 13 novembre 2016

C# How to have only one checkbox checked at a time

I am struggling with why my code is not working. This is for a school assignment. I am allowed to ask for help. I am new to programming. I am using visual studio. I am trying to get it so the user must only be allowed to select one checkbox. I have other checkboxes in this assignment so using last checked will not work. Thanks!

My checkBoxes are named checkBox1, checkBox2,......5

My current code is:

private void checkBox1_Checked(object sender, EventArgs e)
{
    int numberChecked = 0;
    CheckBox[] array = new
    CheckBox[] { checkBox1, checkBox2, checkBox3, checkBox4, checkBox5 };
    for(int i = 0; i < array.Length; i++)
    {
        if(array[i].Checked)
            numberChecked++;
    }
    if(numberChecked > 1)
        MessageBox.Show("You have checked "
           + numberChecked.ToString() + " checkBoxes. Only one is allowed.");
    else
    {

    }




Aucun commentaire:

Enregistrer un commentaire