How to get a checkedlistbox with only single item checked

1 Answer 66 Views
CheckedListBox
Ian
Top achievements
Rank 1
Iron
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.

Tags
CheckedListBox
Asked by
Ian
Top achievements
Rank 1
Iron
Iron
Iron
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or