How do I limit ticks is a checkListBox to one at a time?

Hi,

I am trying to use a checkListBox to select one of three options. I only want any one option at a time.

I have found the following 2 instructions, which, I would have thought, would have done what I want, but I can still check all three boxes.

this.checkedListBox1.CheckOnClick = true;

this.checkedListBox1.SelectionMode = System.Windows.Forms.SelectionMode.One;

Incidentally, I tried

this.checkedListBox1.CheckOnClick = true;

this.checkedListBox1.SelectionMode = System.Windows.Forms.SelectionMode.None;

and this did whatI expected, I could not check any of the boxes.

So, any one know what I am doing wrong

Many thanks for being there

John.



Answer this question

How do I limit ticks is a checkListBox to one at a time?

  • Mervin Lobo

    Thanks John, you are more then welcome!

  • Heng-yi Liu

    You should use RadioButton controls for that purpose. RadioButton group works as you want, but more important every user will know their behavior that only one is active at a time. You can use check box-es also but then you need to execute some trivial code. All you have to do is on checked changed event to set checked property of other two check boxes to false if current check box has checked property equal to true.

  • Tarek Ghazali

    just to add if you are using a radiobutton, then be sure to put it in its own groupbox as by default they are in one group so if you select multiple radiobuttons then they will all be selected, placing it in the groupbox makes the radiobutton in its own group

  • Helio Gama Faria Filho

    Hi PJ

    Thanks for your reply. I will try the Radio buttons, but out of curiosity, it will also have a go with your code. there are somethings in it which I haven't seen before and I could probably use in the future.

    Many thanks

    John


  • Cormac Redmond

    Hi boban,

    Many thanks for your advice, I will give it a try!

    John


  • Burrough

    You can iterate over all the controls on the container and uncheck all boxes that are needed. You can use the method posted below for every checkbox. Just paste this code in your form and wire all checkboxes to it. Here is the example:


    private void checkBox_CheckedChanged(object sender, EventArgs e)
    {
        CheckBox sender = sender as CheckBox;
        if (sender != null && sender.Checked)
        {
            foreach (Control control in this.Controls)
            {
                if (control is CheckBox && control != sender)
                {
                    ((CheckBox)control).Checked = false;
                }
            }
        }
    }

     

    But I agree with Boban.s, you should use RadioButtons instead.



  • Pavan Apuroop

    Hi,

    Thanks for the information. I could have been struggling for ages trying to sort out the groupbox issue.

    Many thanks

    John


  • How do I limit ticks is a checkListBox to one at a time?