Hello

After two days of struggling I have come for some help. I have a listview of contact names that I want to search with text added into a textbox. It can either filter these as the text is entered or by a button search ... I haven't got anywhere with either so far.

  private void btnSearch_Click_1(object sender, EventArgs e)
        {

            for (int i = 0; i < lvContacts.Items.Count; i++)

                if (lvContacts.Items[i].ToString().Contains(txtSearch.Text.ToLower()))
                {
                    //lvContacts.Items[i].Selected; // THIS DOESNT WORK ... as I'm sure you know
                    lvContacts.Items[i].BackColor = Color.CornflowerBlue;
                }    

        }

        private void txtSearch_TextChanged(object sender, EventArgs e)
        {
            lvContacts.SelectedItems.Clear();

            for (int i = 0; i < lvContacts.Items.Count; i++)

                if (lvContacts.Items[i].ToString().Contains(txtSearch.Text.ToLower()))
                {
                    lvContacts.Items[i].BackColor = Color.CornflowerBlue;
                }
        }

Help thank fully received ... John.

Here's one way that uses on the fly searching:

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        foreach (ListViewItem item in listView1.Items)
        {
            //Selected = true, won't show until the listview has focus, but setting it to true puts it in the 
            //SelectedItems collection.
            if (item.Text.ToLower().StartsWith(textBox1.Text.ToLower()))
            {
                item.Selected = true;
                item.BackColor = Color.CornflowerBlue;
                item.ForeColor = Color.White;
            }
            else
            {
                item.Selected = false;
                item.BackColor = Color.White;
                item.ForeColor = Color.Black;
            }

        }
        //When the selection is narrowed to one the user can stop typing
        if (listView1.SelectedItems.Count == 1)
        {
            listView1.Focus();
        }

    }

Edited 3 Years Ago by tinstaafl

//Selected = true, won't show until the listview has focus, but setting it to true puts it in the 
            //SelectedItems collection.
            if (item.Text.ToLower().StartsWith(textBox1.Text.ToLower()))
            {
                item.Selected = true;

    ???-----// how to delete selected item here ------ ???

                item.BackColor = Color.CornflowerBlue;
                item.ForeColor = Color.White;
            }
            else
            {
                item.Selected = false;
                item.BackColor = Color.White;
                item.ForeColor = Color.Black;
            }
        }
        //When the selection is narrowed to one the user can stop typing
        if (listView1.SelectedItems.Count == 1)
        {
            listView1.Focus();
        }

Edited 2 Years Ago by keskin: edit

This question has already been answered. Start a new discussion instead.