Question Looping Through CheckedListBox

digitaldrew

Well-known member
Joined
Nov 10, 2012
Messages
167
Programming Experience
Beginner
Hey everyone..I am trying to loop through all the items in a listbox that are checked (and basically skip over the ones that are unchecked)..The problem is, if there are 7 items that are unchecked before starting - the loop will say its completed when there are still 7 items left in the listbox..It basically counts the number of items that are unchecked - and then stops short by that number..

Here is a little of my code:
VB.NET:
Public Class Form1
Dim count as integer

Private Sub btnStart_Click(sender As System.Object, e As System.EventArgs) Handles btnStart.Click
count = lstSubmissionSites.CheckedItems.Count
End Sub

Private Sub SubmitThread_DoWork(sender As System.Object, e As System.ComponentModel.DoWorkEventArgs) Handles SubmitThread.DoWork
Dim submitworker As BackgroundWorker = CType(sender, BackgroundWorker)
Dim i As Integer
For i = 0 To count - 1
Try
If (SubmitThread.CancellationPending = True) Then
e.Cancel = True
Exit For
Else
'My code to handle each checked item in the listbox
Catch ex as Exception
msgbox(ex.message)
End If
End Try
submitworker.ReportProgress(0, detailshere)
Next
End Sub

Any help here would be appriciated
 
Hi,

You don't need to do any of that. You would just use a For Loop to look for the checked items to be dealt with. Have a look here:-

VB.NET:
For Each strCheckedItem In lstSubmissionSites.CheckedItems
  MsgBox(strCheckedItem.ToString)
Next

By the way, I take it you do know that you have that End If statement in your Catch block which is wrong and is going to cause errors?

Hope that helps.

Cheers,

Ian
 
Hey Ian..thanks so much for your response. I figured it had to be something easy and was thinking it was going to be a for/each loop..I was trying that earlier (minus the strCheckedItem) and couldn't get it to work..I think that was why.

Thanks for the correction on the end if statement I didn't realize it was out of place but found that out pretty quickly.
 
Ian..one other question. If I wanted to convert strCheckedItem to an integer how would I go about doing so? It can't really be done with strCheckedItem because that is only the individual item..I still want to get a count for all checked items as a variable.

Basically I am trying to setselected each checked item as it tests them..The only way I can really think of doing this inside the thread is by invoking it with a variable..
 
Last edited:
Hi,

I think this is what you want. Have a look at this example. It will iterate the whole collection of the CheckedListBox and do some work depending whether it is checked or not:-

VB.NET:
'Dim Count As Integer = lstSubmissionSites.CheckedItems.Count
For intIndex As Integer = 0 To lstSubmissionSites.Items.Count - 1
  If Convert.ToBoolean(lstSubmissionSites.GetItemCheckState(intIndex)) Then
    MsgBox(lstSubmissionSites.Items(intIndex).ToString & " is Checked")
  Else
    MsgBox(lstSubmissionSites.Items(intIndex).ToString & " is NOT Checked")
  End If
Next

Hope that helps.

Cheers,

Ian
 
Thanks for that Ian..That has definitely helped..Although now im facing another problem..

VB.NET:
        For intIndex As Integer = 0 To lstSubmissionSites.Items.Count - 1

            intIndex = r

            'If lstSubmissionSites.InvokeRequired Then
            'lstSubmissionSites.Invoke(Sub() lstSubmissionSites.SelectedIndex = r)
            'End If

            If Convert.ToBoolean(lstSubmissionSites.GetItemCheckState(intIndex)) = True Then

If comment out the invoke as above..then nothing gets selected in the list while it's checking, so you basically have no idea where it is in the list.

If I uncomment out the invoke then it selects the first item in the listbox and sits on it, so you have no idea if it's continuously submitting that one item or if it's moving through the list and the selectedindex just isn't moving with it.

any suggestions? Thanks!
 
Last edited:
Finally got this working. For anyone else who may have problems this is what I did::

VB.NET:
            Dim i As Integer
        For i = 0 To (lstSubmissionSites.Items.Count - 1)
            If lstSubmissionSites.GetItemChecked(i) = True Then
                If lstSubmissionSites.InvokeRequired Then
                    lstSubmissionSites.Invoke(Sub() lstSubmissionSites.SelectedIndex = i)
                End If
                'Perform action to selected item
           End If
       Next
 
Back
Top