Listbox and Checkbox list in VB 2008

Bosox's Avatar
Newbie Member
Hi All,

I am hoping someone out there has encountered this and can help. I've spent way too much time and getting no where. Also, if you can't help but know where I could go for asstance, that would be most appreciated.

I am developing a survey type app that allows the user to check items off in a checkbox list.

The checkbox list are pulled from a db. When the user saves the selected items, it is saved to a different table in the db.

I have a hidden list box on the page. When the page loads, it checks the db to see if that user had been there before and pulls up their previous selections.

My issue is that I can't figure out how to search through the Listbox and compare it to the checkbox list and if they have matching values, set the matched items in the check box list as selected.

Would anyone out there be able to help?

SaswatPadhi's Avatar, Join Date: May 2009
~ Б0ЯИ Τ0 С0δЭ ~
Hi Bosox,

This piece code is your required solution :
Code: VB.NET
For i As Integer = 0 To ListBox1.SelectedItems.Count - 1
      CheckedListBox1.SetItemCheckState((CheckedListBox1.Items.IndexOf(ListBox1.SelectedItems.Item(i))), CheckState.Checked)

Put this inside the ListBox1_SelectedIndexChanged procedure.

The code is quite self-explanatory, but let me briefly explain it :
(1) It scans through all the selected items in the listbox.
(2) For each selected item, it performs a search on the CheckedListBox and retrieves the index of the item.
(3) It then sets the CheckState of that index to CheckState.Checked.