Results 1 to 4 of 4

Thread: [RESOLVED] CheckedListBox -how to allow only 1 check

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2006

    Resolved [RESOLVED] CheckedListBox -how to allow only 1 check

    Another newb question using 2005 Express:

    I created a Checked List Box and entered 3 items that can be individually ticked:


    Right now it's setup so you can tick all the 3 items.

    How can I set it up so you can only tick one box?

    In other words, I do not want the user to be able to tick two or more boxes. The user should only be able to pick one.

    What the "simpliest" way to do this?


  2. #2
    Fanatic Member
    Join Date
    May 2003

    Re: CheckedListBox -how to allow only 1 check

    The simplest way to do this is to remove the ListBox and use Radio Buttons. That is what they are for. When you need to allow the user to select multiple items, use the CheckedListBox. When you need to limit the user to selecting a single item from a population, use Radio Buttons.

    If you just must use the CheckedListBox, then you will have to loop through the items in the CheckedListBox and make sure that only the currently checked item is checked and uncheck any others.

  3. #3
    Junior Member
    Join Date
    Apr 2005

    Resolved Re: CheckedListBox -how to allow only 1 check

    True, but if you don't really know how many options you will have, radio buttons don't really do it I feel.
    To do something like that, I find it easier to use a treeview ( which can have checkboxes aswell).

    Then, you can do something like this:

    VB Code:
    1. private void tvw_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
    2.       {
    3.          foreach (TreeNode tn in tvw.Nodes)
    4.          {
    5.             if (tn == e.Node)
    6.             {
    7.                tn.Checked = true;
    8.             }
    9.             else
    10.             {
    11.                tn.Checked = false;
    12.             }
    13.          }
    14.       }

    Sorry for C#, should be similar enough for vb.
    Last edited by alva; Apr 10th, 2006 at 05:20 PM.

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Sydney, Australia

    Re: CheckedListBox -how to allow only 1 check

    There is no issue using a CheckedListBox for this purpose. You just have to uncheck all other items when an item is checked.
    VB Code:
    1. Private Sub CheckedListBox1_ItemCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemCheckEventArgs) Handles CheckedListBox1.ItemCheck
    2.         If e.NewValue = CheckState.Checked Then
    3.             For i As Integer = 0 To Me.CheckedListBox1.Items.Count - 1 Step 1
    4.                 If i <> e.Index Then
    5.                     Me.CheckedListBox1.SetItemChecked(i, False)
    6.                 End If
    7.             Next i
    8.         End If
    9.     End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Click Here to Expand Forum to Full Width