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?

  • Pockey

    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

  • chipjollyroger

    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


  • seewan

    Thanks John, you are more then welcome!

  • Patrick0

    Hi,

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

    Many thanks

    John


  • Ta1os

    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.

  • Tryst

    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.



  • TobsTec

    Hi boban,

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

    John


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