CheckedListBox allowing only one item to be checked
Asked Answered
I

4

19

In my CheckedListBox app I want to allow only a single item to be checked.

I have these properties already set

checkOnClick = true;
SelectionMode = One;

Any advise will be appreciated

Infante answered 11/5, 2012 at 14:28 Comment(5)
When the user clicks on the listbox, uncheck the other items.Forgiven
would a grouping of radio buttons not suit things better?Majewski
First google hit: social.msdn.microsoft.com/Forums/en-US/winforms/thread/…Transcend
It seems CheckedListBox inherits SelectionMode from ListBox, but ignores that property.Roadbed
@Roadbed Selection is different and checking is different. You can have something selected but not checked.Infante
H
41

uncheck all other items in ItemCheck event as below :

private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e) {
      for (int ix = 0; ix < checkedListBox1.Items.Count; ++ix)
        if (ix != e.Index) checkedListBox1.SetItemChecked(ix, false);
    }
Hesitation answered 11/5, 2012 at 14:30 Comment(5)
Thanks, I thought there must have been a simple property that would do.Infante
even simpler is radio buttons, customers are used to radiobuttons being only one choice and checkboxes being multiple choice, be very careful before you force changes on them like thisBiocellate
@Infante as RhysW said you are better off using radio button...but if you have to use checkboxes then this way would work..Hesitation
@Sam1 Surely that would have been better but in this case I am across an exceptional different scenario where this approach fits best.Infante
@Infante as I said if you have to then there is no reason not to use it :)Hesitation
H
9

the best way to do this is like this:

private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
{
    if (e.NewValue == CheckState.Checked && checkedListBox1.CheckedItems.Count > 0)
    {
        checkedListBox1.ItemCheck -= checkedListBox1_ItemCheck;
        checkedListBox1.SetItemChecked(checkedListBox1.CheckedIndices[0], false);
        checkedListBox1.ItemCheck += checkedListBox1_ItemCheck;
    }
}

no looping is always better.

Hasheem answered 11/7, 2014 at 8:59 Comment(0)
F
1

We can also do this by checkedListBox1_SelectedIndexChanged Event.

private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        int idx = checkedListBox1.SelectedIndex;
        for(int i=0;i<checkedListBox1.Items.Count;i++)
        {
            if(i != idx)
            {
                checkedListBox1.SetItemChecked(i, false);
            }
        }
            
    }
Farnese answered 1/6, 2023 at 6:29 Comment(0)
R
0

Updated to Visual 2019

protected void chb_list_SelectedIndexChanged(object sender, EventArgs e)
        {
            int idx = chb_list.SelectedIndex;
            for (int i = 0; i < chb_list.Items.Count; i++)
            {
                if (i != idx)
                {
                    chb_list.Items[i].Selected = false;
                }
            }
        }
Riegel answered 8/5 at 17:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.