Go4Expert (http://www.go4expert.com/)
-   Visual Basic ( VB ) (http://www.go4expert.com/forums/visual-basic/)
-   -   Listbox and Checkbox list in VB 2008 (http://www.go4expert.com/forums/listbox-checkbox-list-vb-2008-t17862/)

Bosox 5Jun2009 00:44

Listbox and Checkbox list in VB 2008
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 8Jun2009 10:54

Re: Listbox and Checkbox list in VB 2008
Hi Bosox,

This piece code is your required solution :D :
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. :)

All times are GMT +5.5. The time now is 07:34.