check item in a checkedListBox

dubem1

Member
Joined
Dec 5, 2002
Messages
20
I cant figure how to use the checkedlistBox control. Here is my situation.

When I load my form, I add ti the checkedListBox all the possible values. They are all unchecked and came from a DataTable.

Here are the values
ID Value
2045 ItemA
2342 ItemB
2434 ItemC
2342 ItemD

then when I load my record, it contains a collections of id of items to be checked.
Record 1 items to be checked 2045 and 2434
Record 2 items to be checked 2342

The checkedListBox use Indice!! I cant do anythin with indice. I have to deal with my ID

I hope I dont have to do a loop in my checklistbox and check if the id equal the record items collections id!!

Any idea?

Thanks
 
you can get checked items, but you still need to loop through the collection,
Code:
For x = 0 To Listbox1.Items.Count - 1
      If Listbox1.GetItemChecked(x) Then
           you can retrieve the ID (or Value) from the Dataset/DataTable 
(using the X as the Row in the Dataset/DataTable)
      End If
Next
 
No,
SetItemChecked use indice. Maybe I wasnt clear

Here is my four record added to the checkedListBox

ID Name
2045 ItemA
2342 ItemB
2434 ItemC
2342 ItemD

They are all, at first uncheked. If I want to check the second one, I will use CheckedListBox1.SetItemChecked(1, True). Thats ok, but my problem is if I want to check the item corresponding to the Id 2434. I dont know its position is the checkedListBox so I cant use SetItemChecked directly. I need to know first whats the indice(or if yout prefer the index) of the item corresponding to the id 2434. Id there a way to find my item from is ID without doing a loop and check all item one by one?

Thanks
 
Get the row position you need from the Dataset/DataTable, it will correspond with the row of the Listbox.
 
Back
Top