vendredi 19 février 2016

Visual Studio C# - How to let two (or more) checkboxes be checked, rest disabled?

I have the following code to let only 1 checkbox be selected in a groupbox, disabling others, and re-enabling when unchecked.

private void chkBox_CheckedChanged(object sender, EventArgs e)
{
    CheckBox tmp = (CheckBox)sender;
    bool tmpStatus = tmp.Checked;
    foreach (CheckBox chk in this.Controls.OfType<CheckBox>())
    {
        if (tmp.Checked)
        {
            chk.Enabled = false;
            tmp.Enabled = tmpStatus;
        }
        else
        {
            chk.Enabled = true;
            tmp.Enabled = true;
        }
    }
}

How would I be able to make it so if two checkboxes in my groupbox are checked, make the others disabled, and re-enabled when at least one or more are unchecked. Would an int variable help? If so, how do I get that working, because I've been trying and I cannot get it to work.




Aucun commentaire:

Enregistrer un commentaire