Bogdan
Bogdan

Reputation: 382

Show only matched items in ListBox (C#)?

I write quick search function for searching matched items in ListBox:

 for (int i = listBox1.Items.Count - 1; i >= 0; i--)
            {
                if (listBox1.Items[i].ToString().Contains(textBox1.Text))
                {
                    listBox1.SetSelected(i, true);
                }
            }

It selects first matched item. How can I temporarily hide all other items not matching the search query (inside this ListBox1)?

By the way, ListBox contains only numbers, no text stings.

Full source code here.

Upvotes: 1

Views: 344

Answers (1)

LarsTech
LarsTech

Reputation: 81610

Assuming the SelectionMode property is set for MultiExtended, try moving your "if" condition into the boolean parameter:

for (int i = listBox1.Items.Count - 1; i >= 0; i--) {
  listBox1.SetSelected(i, listBox1.Items[i].ToString().Contains(textBox1.Text));
}

Upvotes: 1

Related Questions