Loop through checked items of CheckedListBox

For some reason I can't figure out how to do this. I want to creat a string based on the valuemembers of the checked items.

How can I loop through and get the values?

[169 byte] By [codefund.com] at [2007-12-16]
# 1
If you want the items that are checked use CheckedListBox.CheckedItems. that is a collection of all the items that are currently checked.
codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 2
From there, how can I get the ValueMembers only?
codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 3
I created an array (which is what I want) and got the SelectedValue in it, the only problem is it inputs only the SelectedValue of the first item, whether I use a checkedlistbox or a listbox.

Anyone know how to get to the next selectedvalue?

codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 4
The SelectedItems collection only gives you the values that are currently selected (highlighted), not the items that are checked. You'll need to use the CheckedItems property, which returns a collection of the checked items. If you really want to place the checked items into an array (although you don't really have to do this, for the most part), here's code you can use:
Dim arr As CheckedListBox.CheckedItemCollection = Me.CheckedListBox1.CheckedItems()
Dim items(arr.Count - 1) As Object
arr.CopyTo(items, 0)
Or, another way to write it:
Dim items() As Object

With Me.CheckedListBox1.CheckedItems
ReDim items(.Count - 1)
.CopyTo(items, 0)
End With
But, if you simply want to loop through all the selected items and do something, you could write code like this:
Dim obj As Object
For Each obj In Me.CheckedListBox1.CheckedItems()
' Cast the object as the correct type.
' Not really necessary for strings, usually.
Debug.WriteLine(CType(obj, String))
Next

codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 5
I'm not sure that's exactly it. I have a stored procedure with a parameter that expects this:

3,5,9,12

Basically the ValueMember column of whatever items are checked. I want to get those and those values only into a string to pass to the procedure. The procedure takes those values and puts them in a column in a temp table then checks for any matching values.

the way I was trying to do it, it would only put the value of the first item in the array for as many values were checked ex: 2,2,2,2,2

I could be wrong (highly likely) but they way you have indicated is puting the whole datarow into the array

Thanks, I just didn't explain it well (and maybe still haven't).

codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 6
When you databind (or add the items manually for that matter), the actual objects themselves, DataRowView, String, Integer, whatever is what's actually stored in each item. So by what Ken was showing, when you access the CheckedItems collection, it is a collection of references to the actual objects that you originally put in there. If you originally databinding to a dataview, then each object in the CheckedItems collection would be a DataRowView and from that you could access each field.

I'm guessing (maybe I'm not right) that you're actually looking for a way to loop through all the items and access a .Checked property or something like that (like in ASP.NET CheckBoxList control) and you're not going to find that in the CheckedListBox control in WindowsForms (at least to the best of my knowledge)

codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 7
Wow, that was exactly what I needed to hear. The sad thing is it was right in front of me. The debugger was telling me each item was a DataRowView...I'm not sure how I missed that.

Anyway, since I had a checklistbox with:

cb1.valuemember = interestsID ' ID column in the dataset

I wanted to put the id's of all the checked items into an array...actually just a comma separated string. Thus

Kens code from above plus

While tMembers <= oCount
m_checkstring = m_checkstring & items(tMembers)("interestID")
m_checkstring += ","
tMembers += 1
End While

Again, I don't know how I missed this. i had even done somthing earlier where the Watch indicated I had a value of "interestID".

codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 8
I believe you can do this in a simpler manner, if you want. (Sorry, was offline most of the weekend).

You should be able to modify the code to create your comma-delimited list a little more efficiently. You also end up with an extra comma at the end, right?

I modified the code to retrieve a comma-delimited string, with my demo using a CheckedListBox control bound to a dataset (DataSet11) created using the SQL string "SELECT ProductID, ProductName FROM Products" in the Northwind sample database.
Dim intIndex As Integer
Dim strOut As String = String.Empty

For Each intIndex In _
CheckedListBox1.CheckedIndices
strOut = String.Concat(strOut, ",", DataSet11.Products(intIndex).ProductID)
Next
strOut = strOut.Trim(",")
This may be a bit more efficient (not sure why, but it SEEMS better, using .NET Framework stuff <g>) and will avoid the extra comma problem. You can modify this to meet your own needs, I'm sure.

codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...