0

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
4
Contributors
7
Replies
22
Views
4 Years
Discussion Span
Last Post by GeekPlease
0

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.

0

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

Edited by Learner010

0

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
0

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)

0

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

Edited by Eternal Newbie

0

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

0

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.