2

Need some help with adding filter to my ComboBox drop down list(windows Forms Visual studio 2015)

The drop down is populated as per below:

public ReconciliationReport()
{
    InitializeComponent();
    AppDomain.CurrentDomain.AssemblyResolve += FindDLL;

    this.sRootDirectory = Properties.Resources.sRootDirectory;

    string[] arrProjectList = Directory.GetDirectories(sRootDirectory).Select(Directory => Path.GetFileName(Directory)).ToArray();
    Array.Sort(arrProjectList);

    int iProjectCount = arrProjectList.Length;
    this.DropDownListSize = iProjectCount;

    for (int i = 0; i < iProjectCount; i++)
    {
        SelectJobDropdown.Items.Add(arrProjectList[i]);
    }
}

This gives me a nice drop down list of all current directories. enter image description here

Now, I need to add a filer to show only items which contain a text typed into the ComboBoxitself regardless if the dropdown list itself is open or not.

I have disabled both AutoCompleteMode and AutoCompleteSource as it was not working as expected with the opened droped down list. It was opening additonal list on top the existing one but I could only select from the dropdown under it. See print screen below: enter image description here

The list on top is inactive and I cannot select the text but also does not give an option to display substrings.

Only have one even for the box itself which is

private void SelectJobDropdown_SelectedIndexChanged(object sender, EventArgs e) 
{
    //Plenty of code here 
}

Can someone point in the right direction how to filter the list as I type within the box itself.

Please NOTE I have been using C# for only 3 weeks so might get confused with some of the terminology or other aspects of this language etc.

6
  • Not even a single comment or answer? Dec 6, 2016 at 5:50
  • Possible duplicate of WinForms ComboBox DropDown and Autocomplete window both appear
    – Lucifer
    Dec 6, 2016 at 6:45
  • 1
    This is not a duplicate. I have different requirements sadly. I do not want to display a new list as I type along. I want to filter the main list as type. This solution must search for any text within combobox item and not just the starting characters. The solution you have suggested does include additional list and does not allow for searching any string within an item. So for example if item contains 99999 Hello World, I want to be able to display matching line by typing any of the words in the item and not just 99999. Hope this makes sense. Dec 6, 2016 at 7:55
  • I'm starting to think I will need to add a new event(textChanged or TextAppend) and and search the array for any matches and rebuild the list each time but not too sure if this is the best solution. Cheers Dec 6, 2016 at 7:57
  • I would not rebuild the list each time. I would extract the values based on the filter and bind it to the list. I am working right now on a solution, but have not yet figured it out. My problem is to write into the ComboBox.
    – Mong Zhu
    Dec 6, 2016 at 8:13

3 Answers 3

13

I would suggest to use 2 Lists. 1 for the original values

List<string> arrProjectList;

public ReconciliationReport()
{
    InitializeComponent();
    AppDomain.CurrentDomain.AssemblyResolve += FindDLL;

    this.sRootDirectory = Properties.Resources.sRootDirectory;

    arrProjectList = Directory.GetDirectories(sRootDirectory).Select(Directory => Path.GetFileName(Directory)).ToList();
    arrProjectList.Sort();

    // then just bind it to the DataSource of the ComboBox
    SelectJobDropdown.DataSource = arrProjectList;
    // don't select automatically the first item
    SelectJobDropdown.SelectedIndex = -1;
}

and 1 for the filtered values. In this example I use a TextBox to catch the filter text. In the TextChanged event take the filter text and pull out only those values from the original arrProjectList List. You would need an extra option at the end to reset the binding to the old list if the filter is empty.

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

    List<string> filteredItems = arrProjectList.FindAll(x => x.Contains(filter_param));
    // another variant for filtering using StartsWith:
    // List<string> filteredItems = arrProjectList.FindAll(x => x.StartsWith(filter_param));

    comboBox1.DataSource = filteredItems;

    // if all values removed, bind the original full list again
    if (String.IsNullOrWhiteSpace(textBox1.Text))
    {
        comboBox1.DataSource = arrProjectList;
    }

    // this line will make sure, that the ComboBox opens itself to show the filtered results       
}

EDIT

I found a Solution for typing the filter into the ComboBox directly. The filtering is the same procedure, but using the TextUpdate event it is necessary to unselect the SelectedIndex which is automatically set to the first element after the binding. Then I guess you want to proceed to write your filter (more than just one letter), write the filter back into the ComboBox.Text property and set the cursor position to the end:

private void comboBox1_TextUpdate(object sender, EventArgs e)
{
    string filter_param = comboBox1.Text;

    List<string> filteredItems = arrProjectList.FindAll(x => x.Contains(filter_param));
    // another variant for filtering using StartsWith:
    // List<string> filteredItems = arrProjectList.FindAll(x => x.StartsWith(filter_param));

    comboBox1.DataSource = filteredItems;

    if (String.IsNullOrWhiteSpace(filter_param))
    {
        comboBox1.DataSource = arrProjectList;
    }
    comboBox1.DroppedDown = true;

    // this will ensure that the drop down is as long as the list
    comboBox1.IntegralHeight = true;

    // remove automatically selected first item
    comboBox1.SelectedIndex = -1;

    comboBox1.Text = filter_param;

    // set the position of the cursor
    comboBox1.SelectionStart = filter_param.Length;
    comboBox1.SelectionLength = 0;            
}

Et voilà automatic filtering with a nice display and arrow selection afterwards.

EDIT 2

for case insensitive search you can use this:

List<string> filteredItems = arrProjectList.FindAll(x => x.ToLower().Contains(filter_param.ToLower()));

NOTE:

After the opening of the dropdownlist the cursor will disappear. To prevent this the Cursor.Current has to be set to Cursor.Defualt

comboBox1.DroppedDown = true;
Cursor.Current = Cursors.Default;
20
  • Cool Thanks. I will give that a go and report back. Dec 6, 2016 at 8:51
  • Great. Just reading through it. Cheers Will let you know later. Dec 6, 2016 at 8:54
  • 1
    @TomPisz a simple trick should do it: reset the IntegralHeight property and put it in front of the DroppedDown = true line: comboBox1.IntegralHeight = false; comboBox1.IntegralHeight = true; comboBox1.DroppedDown = true;
    – Mong Zhu
    Jan 4, 2017 at 8:44
  • 1
    amazing! this is pretty relevant and the closest match to an answer I found yet! Good work!
    – sksallaj
    Jul 10, 2019 at 4:01
  • 1
    That was the first thing I did, I'm a good stackoverflow user :-D
    – sksallaj
    Jul 10, 2019 at 12:23
1

In case you are using a Dictionary as data source, the following can be useful. I created a static function in the form to be able to reuse it as I have several ComboBox instances for which I wanted to have the same behavior. Simply call the function from the TextUpdate event passing the control name and the source dictionary.

private static void FilterComboBox(ComboBox combo, Dictionary<int, string> dataSource)
{
  var filter = combo.Text;
  if (string.IsNullOrWhiteSpace(filter))
    return;

  var filteredItems = dataSource.Where(kv => kv.Value.Contains(filter)).ToDictionary(k => k.Key, k => k.Value);

  combo.DisplayMember = "Value";
  combo.ValueMember = "Key";
  combo.DataSource = new BindingSource(filteredItems, null);

  // this will ensure that the drop down is as long as the list
  combo.IntegralHeight = false;
  combo.IntegralHeight = true;

  combo.DroppedDown = true;
  
  // remove automatically selected first item
  combo.SelectedIndex = -1;
  combo.Text = filter;

  // set the position of the cursor
  combo.SelectionStart = filter.Length;
  combo.SelectionLength = 0; 
}
0

i added keyPress event and its working ok for me. it's just closing the original dropDown list.

private void combobox_KeyPress(object sender, KeyPressEventArgs e)
{
    combobox.DroppedDown = false;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.