i want to traverse a checkedlistbox using for each loop
for example :- if i want to see that a particular item name exist in checkedlistbox or not. so i think that i need to use for each loop but really don't know how to traverse checkedlistbox using for each loop.

for each "here i dont know what to use" in checkedlistbox1
    if <condition is true> then
        messagebox("Item found")
    End if
next

Recommended Answers

All 7 Replies

If you just want to know if an item exists you can use the Contains method of the Items collection,CheckedListBox1.Items.Contains("MyItem"). This produces a boolean true or false.

but if i just want to know at which location item is found Then ?

Here's a code of mine, almost solving the same problem as yours:

        If TextBox1.Text <> "" Then
            Ftxt = TextBox1.Text
            For i As Integer = 0 To Me.CheckedListBox1.Items.Count - 1
                If CheckedListBox1.Items(i).ToString.Trim = Ftxt.Trim Then
                    CheckedListBox1.SetItemChecked(i, True)
                End If
            Next
        End If

thanx for the code but still i need to do this using for each loop not for next(it will provide the opportunities to learn something new, as i am beginner to VB.NET)

Happy to see someone's learning new things in VB.Net. I changed the code so you can understand it better:

        For Each ci As String In CheckedListBox1.Items
            If ci.Trim Like Ftxt.Trim Then
                MsgBox(ci)
            End If
        Next

thanx , its really working and helped
one more thing i want to consult here is that "does Like is same as = in conditions ?"

It does almost the same thing except that Like can be use to compare string with wildcards.

For example,

1234 = 123 ' The answer here is false while
1234 Like 123* ' The answer here is true

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.