0
votes

I have a two ListBoxes listbox1 and listbox2. I want to get all the duplicate items in ListBox1 that i'd search in a TextBox and put in into ListBox2 and when all the duplicate item's that i'd search is in ListBox2 its automatically count please help me.

For example, items in ListBox1

DOG
DOG
DOG
CAT
CAT

When I type DOG in TextBox all the DOG in ListBox1 will be copied to ListBox2. How can I do it?

I tried this

Dim check As Boolean
For Each item In ListBox1.Items
    check = ListBox1.FindStringExact(item)
    ListBox2.Items.Add(item)
Next

i also try this but its wrong it count's the line before the exact word that i'd search. for example DOG DOG DOG CAT CAT i'd search CAT in textbox the output in listbox2 is 3 here's my code :

Dim check As String

    check = ListBox1.FindStringExact(TextBox1.Text)
    ListBox2.Items.Add(check)
2

2 Answers

1
votes
listBox2.Items.AddRange(listBox1.Items.Cast(Of ListItem)().Where(Function(x) x.Text = TextBox1.Text ).ToArray(Of ListItem)())
0
votes

Try the below code in a button click after entering value in text box.

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim searchFor As String = TextBox1.Text
    For Each item In ListBox1.Items
        If item = searchFor Then
            ListBox2.Items.Add(item)
        End If
    Next

    Do While ListBox1.Items.Contains(searchFor)
        ListBox1.Items.Remove(searchFor)
    Loop
End Sub