how to filter comboBox by any part of the list.?

ahmed omar 181 Reputation points
2023-05-13T12:30:33.6866667+00:00

Hello,

the application is built in Winfrom C#. and I have a ComboBox containing a list of items

comboBox1.Items.Add("first One "); 
comboBox1.Items.Add("second term");
 comboBox1.Items.Add("extra one"); 

I need to search for any word in the list, but currently, the filter is only filtering by the first word.

so if I wrote "One" the result should be two items.

if I wrote "second" the result should be one item

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,468 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
8,186 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Castorix31 71,621 Reputation points
    2023-05-14T08:14:19.9333333+00:00

    You can so domething like this :

           private readonly List<string> items = new List<string> { "first One ", "second term", "extra one" };
           private BindingList<string> _bindinglist;
    

    In Form_Load :

                _bindinglist = new BindingList<string>(items);
                comboBox1.DataSource = _bindinglist;
    

    Test in a Button click

                    string sKey = "One";
                    Console.WriteLine(sKey);
                    var newList = _bindinglist.Where(c => c.IndexOf(sKey, StringComparison.OrdinalIgnoreCase) >= 0);
                    foreach (var sItem in newList)
                    {
                        Console.WriteLine("\t" + sItem);
                    }
                    sKey = "second";
                    Console.WriteLine(sKey);
                    newList = _bindinglist.Where(c => c.IndexOf(sKey, StringComparison.OrdinalIgnoreCase) >= 0);
                    foreach (var sItem in newList)
                    {
                        Console.WriteLine("\t" + sItem);
                    }
    
    0 comments No comments