lolo
lolo

Reputation: 39

How to disable any checkbox from checking if another one is checked in WinFormApp

How to disable any checkbox from checking if another one is checked in WinFormApp ? I need one at a time to be checked!

Upvotes: 0

Views: 1832

Answers (2)

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236328

Use radio buttons. If you really don't want to use them, then subscribe all checkboxes to following CheckedChanged event handler:

private void checkBox_CheckedChanged(object sender, EventArgs e)
{
    CheckBox checkBox = sender as CheckBox;

    if (checkBox.Checked)
        foreach (var other in Controls.OfType<CheckBox>().Where(c => c != checkBox))
            other.Checked = false;
}

Upvotes: 0

Kye
Kye

Reputation: 6299

Sounds like you should be using radio buttons instead. To do that with check boxes you'll need to manually uncheck the boxes on the .checked event.

Upvotes: 1

Related Questions