How to get a checkedlistbox with only single item checked

1 Answer 857 Views
CheckedListBox
Ian
Top achievements
Rank 2
Bronze
Iron
Iron
Ian asked on 28 Jan 2022, 09:31 AM

Very simple problem. 

How to get a CLB with only one item ever checked? There's a simple property to allow only single-select, but not one for 'only allow one box to be checked at a time'

How to do this, from inside the 'ItemCheckedChanged' event handler, without causing a stack overflow, as the previously cheked items are un-checked?

I feel this must be really easy - am I just missing something?

1 Answer, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 01 Feb 2022, 10:31 AM
Hello, Ian,   

There is no straightforward way to keep only single item checked in a RadCheckedListBox. However, this can be easily achieved by the following approach:
        public RadForm1()
        {
            InitializeComponent();

            this.radCheckedListBox1.ItemCheckedChanged += radCheckedListBox1_ItemCheckedChanged;
        }

        private void radCheckedListBox1_ItemCheckedChanged(object sender, ListViewItemEventArgs e)
        {
            this.radCheckedListBox1.ItemCheckedChanged -= radCheckedListBox1_ItemCheckedChanged;
            ListViewDataItem itemToUncheck = null;
            foreach (ListViewDataItem item in this.radCheckedListBox1.CheckedItems)
            {
                if (e.Item != item)
                {
                    itemToUncheck = item;
                    break;
                }
            }
            if (itemToUncheck!=null)
            {
                 itemToUncheck.CheckState = Telerik.WinControls.Enumerations.ToggleState.Off;
            }
           
            this.radCheckedListBox1.ItemCheckedChanged += radCheckedListBox1_ItemCheckedChanged;
        }

I hope this information helps. If you need any further assistance please don't hesitate to contact me. 

Regards,
Dess | Tech Support Engineer, Principal
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Fritz
Top achievements
Rank 1
commented on 26 Jul 2023, 05:18 PM

Hello Dess, 

is there now an easier way to do this?

If not, I suggest a flag to set this behaviour. e.g. AllowMultipleChecks

Best regards
Fritz

Dinko | Tech Support Engineer
Telerik team
commented on 29 Jul 2023, 01:24 PM

We understand your point but with the current implementation of the control, we can't suggest a more suitable approach. You are right that introducing a property to allow only one checkbox could be a good improvement to the checkbox logic. I will forward your request to our development team for further discussion. 
Tags
CheckedListBox
Asked by
Ian
Top achievements
Rank 2
Bronze
Iron
Iron
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or