Loop through checked items of CheckedListBox
How can I loop through and get the values?
How can I loop through and get the values?
Anyone know how to get to the next selectedvalue?
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
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).
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)
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".
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.