none
CheckedListBox - allow only one item to be selected RRS feed

  • 質問

  • Hi,

     

    I am using CheckedListBox and I want to allow the user to select only one item in the listbox.

     

    I used  cbList.SelectionMode = SelectionMode.One; to disable it, but it doesnt work

     

    I am still able to select second item after I select the first item..

     

    am I missing anything ?

     

    Thank you,

    2007年4月2日 16:24

回答

  • Just uncheck the other items when you see one getting checked:

        private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e) {
          if (e.NewValue == CheckState.Checked)
            for (int ix = 0; ix < checkedListBox1.Items.Count; ++ix)
              if (e.Index != ix) checkedListBox1.SetItemChecked(ix, false);
        }

    2007年4月3日 2:35
    モデレータ

すべての返信

  • Hi,

    that's because selecting is not the same thing as checking items. You can select more items at once, but only check one at the time. To allow only one checked item at once, you can manually inspect the CheckedItems.Count and prevent user from checking additional items:

     

    Code Snippet

    private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)

    {

        if (checkedListBox1.CheckedItems.Count >= 1 && e.CurrentValue != CheckState.Checked)

        {

            e.NewValue = e.CurrentValue;

            MessageBox.Show("You can only check one item");

        }

    }

     

     

    Andrej

    2007年4月2日 21:30
  • Just uncheck the other items when you see one getting checked:

        private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e) {
          if (e.NewValue == CheckState.Checked)
            for (int ix = 0; ix < checkedListBox1.Items.Count; ++ix)
              if (e.Index != ix) checkedListBox1.SetItemChecked(ix, false);
        }

    2007年4月3日 2:35
    モデレータ
  • hi frds...dis is really a nice answer thanx buddy......
    2009年10月20日 5:04
  • thanx a lot....it works correctly.... :)
    2011年7月22日 10:26
  • thanx a lot... :)
    2011年7月22日 10:32
  • The above answer loops over all the items in the collection not very efficient, as the collection of items in the listbox grows the loop will take longer and longer to set all the items to unchecked state. The correct solution is to get the list of items that are checked and uncheck only that specific item.


            private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
            {
                // Ensure that we are checking an item
                if (e.NewValue != CheckState.Checked)
                {
                    return;
                }

                // Get the items that are selected
                CheckedListBox.CheckedIndexCollection selectedItems = this.checkedListBox1.CheckedIndices;

                // Check that we have at least 1 item selected
                if (selectedItems.Count > 0)
                {
                    // Uncheck the other item
                    this.checkedListBox1.SetItemChecked(selectedItems[0], false);
                }
          }

    • 回答の候補に設定 Olly_Cologne 2014年2月14日 20:17
    2013年8月26日 15:36
  • In this scenario RadioButtonList1 or OptionButtonList is better control..
    2019年8月31日 4:30
  • In this scenario RadioButtonList1 or OptionButtonList is better control..
    These controls do not exists in Windows Forms, also this post is old, please keep to post within the past several months when responding, thanks for understanding.

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    2019年8月31日 10:03
    モデレータ