Jedi
Jedi

Reputation: 153

Check if listbox contains textbox

I know I could use .FindString for this but for some reason it is not working.

Basically,if listbox items contains just a PART of textbox text,it does action.

Here's the example of not-working code :

Dim x As Integer = -1
        x = ListBox1.FindString(TextBox1.Text)
        If x > -1 Then
            'dont add
            ListBox2.Items.Add("String found at " & x.ToString)
        Else

        End If

Upvotes: 2

Views: 14491

Answers (1)

user2480047
user2480047

Reputation:

The FindString method returns the first item which starts with the search string (MSDN). If you want to match the whole item, you would have to use FindStringExact (MSDN). If you want to perform more complex searches, you would have to iterate through all the elements in the ListBox.

UPDATE: Code delivering the exact functionality expected by the OP.

For i As Integer = 0 To ListBox1.Items.Count - 1
    If (ListBox1.Items(i).ToString.Contains(TextBox1.Text)) Then
        ListBox2.Items.Add("String found at " & (i + 1).ToString) 'Indexing is zero-based
        Exit For
    End If
Next

Upvotes: 4

Related Questions