locked
Make selection in CheckedListBox exclusive.. RRS feed

  • Question

  • Hello,

    In an application I build a list of CheckedListBox and display it to the user... I want to make the selection exclusive to only one... I noticed that you can check more that one item in the CheckedListBox... How can I stop that and as the user check the second box I uncheck his/her first selection.
    SForm.ChkBoxPts.Items.Add("Paarsa")
    SForm.ChkBoxPts.Items.Add("Jeff")
    I.e. If I display the above two items or may be even more, I would like to force the user only select one and only one.

    Regards,

    Jeff P.


    Jeff Paarsa
    Wednesday, April 29, 2009 6:42 PM

Answers

  • Try this code out.  YOu miliage may vary :)

     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
           
            For Icount = 0 To 10
                Me.CheckedListBox1.Items.Add("Item #" & Icount)
            Next
    
            AddHandler CheckedListBox1.ItemCheck, AddressOf SetExclusiveCheckInList
    
        End Sub
    
        Private Sub SetExclusiveCheckInList(ByVal sender As Object, ByVal e As ItemCheckEventArgs)
    
            Dim MyList As CheckedListBox = Nothing
    
            Try
    
                MyList = CType(sender, CheckedListBox)
    
                RemoveHandler CheckedListBox1.ItemCheck, AddressOf SetExclusiveCheckInList
    
    
                For Icount = 0 To MyList.Items.Count - 1
                    MyList.SetItemChecked(Icount, Icount.Equals(e.Index))
                Next
    
    
                AddHandler CheckedListBox1.ItemCheck, AddressOf SetExclusiveCheckInList
    
            Catch ex As Exception
    
            End Try
    
    
        End Sub
    • Marked as answer by Jeff Paarsa Thursday, April 30, 2009 7:12 PM
    Wednesday, April 29, 2009 7:24 PM
  • Thansk
    Jeff Paarsa
    • Marked as answer by Jeff Paarsa Thursday, April 30, 2009 7:11 PM
    Thursday, April 30, 2009 7:11 PM

All replies

  • Thanks nobugz,

    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);
        }


    Thanks, A.m.a.L | [Remember to click "mark as answered" when you get a correct reply to your question]
    Wednesday, April 29, 2009 6:59 PM
  • Try this code out.  YOu miliage may vary :)

     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
           
            For Icount = 0 To 10
                Me.CheckedListBox1.Items.Add("Item #" & Icount)
            Next
    
            AddHandler CheckedListBox1.ItemCheck, AddressOf SetExclusiveCheckInList
    
        End Sub
    
        Private Sub SetExclusiveCheckInList(ByVal sender As Object, ByVal e As ItemCheckEventArgs)
    
            Dim MyList As CheckedListBox = Nothing
    
            Try
    
                MyList = CType(sender, CheckedListBox)
    
                RemoveHandler CheckedListBox1.ItemCheck, AddressOf SetExclusiveCheckInList
    
    
                For Icount = 0 To MyList.Items.Count - 1
                    MyList.SetItemChecked(Icount, Icount.Equals(e.Index))
                Next
    
    
                AddHandler CheckedListBox1.ItemCheck, AddressOf SetExclusiveCheckInList
    
            Catch ex As Exception
    
            End Try
    
    
        End Sub
    • Marked as answer by Jeff Paarsa Thursday, April 30, 2009 7:12 PM
    Wednesday, April 29, 2009 7:24 PM
  • Thansk
    Jeff Paarsa
    • Marked as answer by Jeff Paarsa Thursday, April 30, 2009 7:11 PM
    Thursday, April 30, 2009 7:11 PM
  • Thanks I used this methode and it worked out okay....
    Jeff Paarsa
    Thursday, April 30, 2009 7:12 PM