locked
Populating checkbox list RRS feed

  • Question

  • User237232814 posted


    Hi! I'm really new in .Net and what I'm trying to do is the following.

    I have a textbox control with a button then underneath of it a have a checkboxlist control and another button. Basically what I want to do is when a User enters something on the textbox and then press the Add button the text they enter gets added to the checkbox list. This I was able to do with no problems, The problems comes when I try to remove the selected Items on the checkbox list. I have the following code.


    Protected Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAdd.Click
            Dim myPhoneNumber As String = txtPhoneNumber.Text
            Dim myListOfPhoneNumbers As CheckBoxList = CType(chkPhoneNumbers, CheckBoxList)
            chkPhoneNumbers.Items.Add(Convert.ToString(myPhoneNumber))
    
            txtPhoneNumber.Text = Nothing
    End Sub
    
    Protected Sub btnDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDelete.Click
            Dim myListOfPhoneNumbers As CheckBoxList = CType(chkPhoneNumbers, CheckBoxList)
    
            chkPhoneNumbers.Items.Remove(chkPhoneNumbers.SelectedValue)
    
    End Sub

    The problem with the btnDelete_Click code is that If a select several items on the checkboxlist control only one of them gets removed. I tried this: but I get an error

    Protected Sub btnDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDelete.Click
            Dim myListOfPhoneNumbers As CheckBoxList = CType(chkPhoneNumbers, CheckBoxList)
    
            For Each Item As ListItem In chkPhoneNumbers.Items
                If Item.Selected Then
                    chkPhoneNumbers.Items.Remove(chkPhoneNumbers.SelectedValue)
                End If
            Next
    
    End Sub

    Any help would greatly be appreciated


    Tuesday, January 26, 2010 10:28 AM

Answers

  • User-952121411 posted

    Yes the idea was correct, but the implementation was a bit off.  You will not be able to modify the collection in a For Each loop like that because the enumeration of items changed.  A solution is to loop from the end to the beginning.  Look at the code sample given in the following link:

    Collection was modified; enumeration operation may not execute:

    http://www.vbdotnetforums.com/vb-net-general-discussion/3833-collection-modified-enumeration-operation-may-not-execute.html

    The code will probably change to something like this:

    For i As Integer = (chkPhoneNumbers.Items.Count - 1) To 0 Step -1
        If chkPhoneNumbers.Items(i).Selected Then     
            chkPhoneNumbers.Items.RemoveAt(i)
        End If
    Next


     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 26, 2010 4:58 PM

All replies

  • User-925904572 posted

    Hi,

     

    From your code, results are as expeted. You pass the same value in your remove function, even though you loop.

    Your second function should work once you have the proper types plugged in.

    01.Protected Sub btnDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDelete.Click   
    02.        Dim myListOfPhoneNumbers As CheckBoxList = CType(chkPhoneNumbers, CheckBoxList)   
    03.  
    04.        For Each Item As ListItem In chkPhoneNumbers.Items   
    05.            If Item.Selected Then  
    06.                chkPhoneNumbers.Items.Remove(chkPhoneNumbers.SelectedValue)    ' <<== even though you are looping, you are always passing the same value
                         chkPhoneNumbers.Items.Remove(Item)  ' <<== this should work
                         chkPhoneNumbers.Items.Remove(Item.Text)  '' << == this should also work
                         chkPhoneNumbers.Items.Remove(Item.Value) '' <<== this will work only if you assign a value to the item when adding....currently, you only add the text
    07.            End If  
    08.        Next  
    09.  
    10.End Sub 


    Please mark as answered if this post has helped you

     

     

     

    Tuesday, January 26, 2010 12:16 PM
  • User237232814 posted

    dzieba thanks for you reply, however I tried what you instructed and I get the following error:

    Collection was modified; enumeration operation may not execute.

    The line number is on the Next command. 


    Thanks again

    Carlos

    Tuesday, January 26, 2010 4:10 PM
  • User-952121411 posted

    Yes the idea was correct, but the implementation was a bit off.  You will not be able to modify the collection in a For Each loop like that because the enumeration of items changed.  A solution is to loop from the end to the beginning.  Look at the code sample given in the following link:

    Collection was modified; enumeration operation may not execute:

    http://www.vbdotnetforums.com/vb-net-general-discussion/3833-collection-modified-enumeration-operation-may-not-execute.html

    The code will probably change to something like this:

    For i As Integer = (chkPhoneNumbers.Items.Count - 1) To 0 Step -1
        If chkPhoneNumbers.Items(i).Selected Then     
            chkPhoneNumbers.Items.RemoveAt(i)
        End If
    Next


     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 26, 2010 4:58 PM
  • User237232814 posted

    Thanks a lot it worked...

    Tuesday, January 26, 2010 5:17 PM