Disable multiple selection in Checkboxlist

checkboxlist disable multiple selection Suppose if you want user to select only open item from checkboxlist here is the solution, Checkboxlist disable multiple selection and allow only one item to select by the user.
And retrieve check value through switch statement.

Download

Example Source Snippets - CheckedListBox.zip

namespace : using System.Windows.Forms

Disable multiple selection | retrieve Checkboxlist value through switch statement


        private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
        {
            // return it none selected
            if (e.NewValue != CheckState.Checked)
            {
                return;
            }
            //get check items value from the list
            System.Windows.Forms.CheckedListBox.CheckedIndexCollection selectedItems = this.checkedListBox1.CheckedIndices;

            //if more than one item we are going to remove by setting false
            if (selectedItems.Count > 0)
            {
                // Uncheck the other item
                this.checkedListBox1.SetItemChecked(selectedItems[0], false);
            }
        }

        private void LoopAndGetValue()
        {
            System.Windows.Forms.CheckedListBox.CheckedIndexCollection selectedItems = this.checkedListBox1.CheckedIndices;   

            if (selectedItems.Count > 0)//dummy
            {              
                switch (checkedListBox1.SelectedIndex)
                {
                    case 0:
                        lblValue.Text = "One";
                        break;
                    case 1:
                        lblValue.Text = "Two";
                        break;
                    case 2:
                        lblValue.Text = "Three";
                        break;
                    case 3:
                        lblValue.Text = "Four";
                        break;
                    case 4:
                        lblValue.Text = "Five";
                        break;
                    case 5:
                        lblValue.Text = "Six";
                        break;
                    case 6:
                        lblValue.Text = "Seven";
                        break;   
                }
            }
            else
            {
                lblValue.Text = "xxx";
            }
        }

                 

smartsnipps.ecomparefiles.com © 2019, All Rights Reserved | Disclaimer: smartsnipps.eCompareFiles.com is free to use any code snippets without guarantee