Rayane Staszewski
Rayane Staszewski

Reputation: 122

Check if a string contains a certain word (but not all)

it's me again! (starts to know each other now ;D) I create a search bar in my application (contact manager). As soon as you start typing in this bar, the following code is updated with each letter:

private void Text_search_TextChanged(object sender, EventArgs e)
{
    if(String.IsNullOrWhiteSpace(text_search.Text))
    {
        label_rechercher.Show();
    }
    else
    {
        label_rechercher.Hide();
        //here
        for(int i = 0; i<nomContact.Count; i++)
        {
            if(text_search.Text.Contains(nomContact[i]))
            {
                MessageBox.Show(nomContact[i]);
            }
        }
    }
}

"List nameContact" contains the names of all contacts

So it looks for contacts except that my messageBox will only activate when the full first name is given. So here's my question: How can we make it so that it is a letter, a word or a sentence that it proposes things and not only when all the word is found?

Thank you :)

Upvotes: 0

Views: 75

Answers (1)

P.N.
P.N.

Reputation: 645

You can use the FindAll method to give you all the results that match your criteria. In your case, a name that contains the text that was typed.

var results = nomContact.FindAll(x => x.Contains(text_search.Text));

Upvotes: 2

Related Questions