How to get the checked items in checklistbox

ABCDE picture ABCDE · Nov 2, 2016 · Viewed 8k times · Source

I have a checklistbox and the items of it came from my database (tbl_Section) so it loads all the Section Number (Primary Key). I have 5 Section Numbers, and 3 of it will be assigned to only one teacher. I'm thinking of using While-statement but i dont know how.

To make it simpler to you, this is what i need to do:

While //index(number) is checked
      //do something
Else (i know it should not be ELSE, but i dont know what keyword is to be used)
      //do something
End While

Thanks a lot!

Answer

Martin Verjans picture Martin Verjans · Nov 2, 2016

What you want to do is iterate through every item in your checkbox. For each item, you check if it is checked, then you act accordingly :

'We will run through each indice
For i = 0 To CheckedListBox1.Items.Count - 1
    'You can replace As Object by your object type
    'ex : Dim Item As String = CType(CheckedListBox1.Items(i), String)
    Dim Item As Object = CheckedListBox1.Items(i)

    'We ask if this item is checked or not
    If CheckedListBox1.GetItemChecked(i) Then
        'Do something if Item is checked
    Else
        'Do something else if Item is not checked
    End If
Next