Results 1 to 6 of 6

Thread: [RESOLVED] Error on setting checkstate in a CheckedListBox control

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2012
    Location
    Las Vegas, NV
    Posts
    41

    Resolved [RESOLVED] Error on setting checkstate in a CheckedListBox control

    I have a CheckedListBox control that I fill with DataGridView Column HeaderText values. If these columns are visible, I would like to set the CheckedListBox Items to "Checked". My code is as follows:

    Code:
    For Each col As DataGridViewColumn In frmTimingP2P.dgvOverview.Columns
        If col.Visible = True Then
            For Each item In clbOverviewColumnOrder.Items
                Dim intItemIndex As Integer = clbOverviewColumnOrder.Items.IndexOf(item)
                If col.HeaderText = item.ToString Then
                    clbOverviewColumnOrder.SetItemCheckState(intItemIndex, CheckState.Checked)
                End If
            Next
        End If
    Next
    Whenever this code runs, I get the following error:

    "List that this enumerator is bound to has been modified. An enumerator can only be used if the list does not change."

    What causes this? How can I get around this issue?

    Thanks

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,380

    Re: Error on setting checkstate in a CheckedListBox control

    In this case you cannot use a foreach loop, it's like trying to remove controls using a foreach loop. Instead you have to loop through the items using a for loop and looping them backwards. Take a look at this:
    Code:
            Dim checked As Boolean = True 'or false
            For i As Integer = CheckedListBox1.Items.Count - 1 To 0 Step -1
                CheckedListBox1.SetItemChecked(i, checked)
            Next
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  3. #3
    Fanatic Member
    Join Date
    Feb 2013
    Posts
    985

    Re: Error on setting checkstate in a CheckedListBox control

    looks like it doesnt want you to edit the control while your using the enumerator(thing keeping track of the ForEach/Next loop)

    try For/Next loop instead
    Yes!!!
    Working from home is so much better than working in an office...
    Nothing can beat the combined stress of getting your work done on time whilst
    1. one toddler keeps pressing your AVR's power button
    2. one baby keeps crying for milk
    3. one child keeps running in and out of the house screaming and shouting
    4. one wife keeps nagging you to stop playing on the pc and do some real work.. house chores
    5. working at 1 O'clock in the morning because nobody is awake at that time
    6. being grossly underpaid for all your hard work


  4. #4

    Thread Starter
    Member
    Join Date
    Aug 2012
    Location
    Las Vegas, NV
    Posts
    41

    Resolved Re: Error on setting checkstate in a CheckedListBox control

    Got it. I revised my code and ended up with the following:

    Code:
    Dim intCurrentItemIndex As Integer = clbOverviewColumnOrder.Items.Count - 1
            Do While intCurrentItemIndex >= 0
                Dim strCurrentItem As String = clbOverviewColumnOrder.Items(intCurrentItemIndex)
                For Each col As DataGridViewColumn In frmTimingP2P.dgvOverview.Columns
                    If col.HeaderText = strCurrentItem Then
                        If col.Visible = True Then
                            clbOverviewColumnOrder.SetItemCheckState(intCurrentItemIndex, CheckState.Checked)
                        Else
                            clbOverviewColumnOrder.SetItemCheckState(intCurrentItemIndex, CheckState.Unchecked)
                        End If
                    End If
                Next
                intCurrentItemIndex -= 1
            Loop
    It works perfectly. Thanks!

  5. #5
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,380

    Re: [RESOLVED] Error on setting checkstate in a CheckedListBox control

    If I could make a suggestion, don't use a do while in this situation. While it may work, it's quicker to use the for loop I showed above. Pretty much anytime you can use a for loop rather than a do loop, then choose the for loop.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  6. #6

    Thread Starter
    Member
    Join Date
    Aug 2012
    Location
    Las Vegas, NV
    Posts
    41

    Re: [RESOLVED] Error on setting checkstate in a CheckedListBox control

    That is interesting. I was unaware that a For...Next loop was any quicker than a Do...Loop.

Tags for this Thread

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