C# Searching a listBox

14,603

Solution 1

Filter the listbox. Try this:

    List<string> items = new List<string>();
    private void Form1_Load(object sender, EventArgs e)
    {
        items.AddRange(new string[] {"Cat", "Dog", "Carrots", "Brocolli"});

        foreach (string str in items) 
        {
            listBox1.Items.Add(str); 
        }
    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        listBox1.Items.Clear();

        foreach (string str in items) 
        {
            if (str.StartsWith(textBox1.Text, StringComparison.CurrentCultureIgnoreCase))
            {
                listBox1.Items.Add(str);
            }
        }
    }

Solution 2

Here's an pretty good example: http://www.java2s.com/Code/CSharp/Components/UseanAutocompleteComboBox.htm

Solution 3

Rudimentary example; however this should get you started...

    public partial class Form1 : Form
    {
        List<String> _animals = new List<String> { "cat", "carrot", "dog", "goat", "pig" };

        public Form1()
        {
            InitializeComponent();

            listBox1.Items.AddRange(_animals.ToArray());
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            String search = textBox1.Text;

            if (String.IsNullOrEmpty(search))
            {
                listBox1.Items.Clear();
                listBox1.Items.AddRange(_animals.ToArray());
            }

            var items = (from a in _animals
                        where a.StartsWith(search)
                        select a).ToArray<String>();

            listBox1.Items.Clear();
            listBox1.Items.AddRange(items);
        } 
    }

Solution 4

For getting result which the asked expects, you have to use Contains method instead StartWith method. Like this:-

private void textBox1_TextChanged(object sender, EventArgs e)
    {
        listBox1.Items.Clear();

        foreach (string str in items)
        {
            if (str.ToUpper().Contains(textBox1.Text.ToUpper()))
            {
                listBox1.Items.Add(str);
            }
        }
    }

I was in search for this.

Share:
14,603
Luke Berry
Author by

Luke Berry

Lead Developer @payzip #SOreadytohelp

Updated on June 04, 2022

Comments

  • Luke Berry
    Luke Berry almost 2 years

    I have a large amount of items in a listBox called listBox1. I also have a textBox (textBox1) at the top. I want to be able to type into the textBox and the listBox searches through it's item's and finds ones that contain what I am typing.

    For example, say the listBox contains

    "Cat"

    "Dog"

    "Carrot"

    and "Brocolli"

    If I start typing the letter C, then I want it to show both Cat and Carrot, when I type a it should keep showing them both, but when I add an r it should remove Cat from the list. Is there anyway to do this?

  • Aaron McIver
    Aaron McIver over 13 years
    -1 While similar this is a ComboBox; the OP has a ListBox coupled with a TextBox. Assuming the OP is going for something similar to filter as you type behavior ala the ICollectionView.Filter
  • Ziv
    Ziv over 13 years
    Wouldn't the Clear and AddRange be bad for performance if the list is long? Or because it's all references it doesn't matter?
  • Aaron McIver
    Aaron McIver over 13 years
    There would be a performance knock based on the list size at some point; however you could scale up to a decent size before a detriment in performance is noticed.
  • Aaron McIver
    Aaron McIver over 13 years
    If you are not going to use AddRange you should be using BeginUpdate/EndUpdate to prohibit unneeded drawing
  • Luke Berry
    Luke Berry about 13 years
    I like this a lot. Thankyou!
  • Luke Berry
    Luke Berry about 13 years
    Do you know what that decent size would be? Or whereabouts? I expect at least a few hundred items in the list.
  • Aaron McIver
    Aaron McIver about 13 years
    @LukeBerry You will be fine with a few hundred