C#将过滤器添加到组合框下拉列表中

时间:2016-12-05 22:35:08

标签: c# winforms filter combobox dropdownbox

在我的ComboBox下拉列表中添加过滤器需要一些帮助(Windows窗体Visual Studio 2015)

下拉列表如下所示:

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]);
    }
}

这给了我一个很好的下拉列表,列出了所有当前目录。 enter image description here

现在,我需要添加一个文件管理器,以便只显示包含在ComboBox本身中输入的文本的项目,无论下拉列表本身是否打开。

我已将AutoCompleteModeAutoCompleteSource都停用,因为打开的下拉列表无法正常工作。它在现有的顶部打开了附加列表,但我只能从它下面的下拉列表中选择。见下面的打印屏幕: enter image description here

顶部的列表处于非活动状态,我无法选择文本,但也没有显示子字符串的选项。

只有一个盒子本身就是

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

当我在框中输入时,有人能指向正确的方向来过滤列表。

请注意我一直使用C#只有3周,所以可能会混淆一些术语或该语言的其他方面等。

2 个答案:

答案 0 :(得分:6)

我建议使用2个列表。 1表示原始值

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;
}

和1表示过滤值。在此示例中,我使用TextBox来捕获过滤器文本。在TextChanged事件中,获取过滤器文本,并仅从原始arrProjectList列表中提取这些值。如果过滤器为空,则最后需要一个额外的选项来重置绑定到旧列表。

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       
}

编辑

我找到了直接在ComboBox中输入过滤器的解决方案。过滤是相同的过程,但是使用TextUpdate事件时,必须取消选择绑定后自动设置为第一个元素的SelectedIndex。然后我想你想继续编写你的过滤器(不只是一个字母),把过滤器写回ComboBox.Text属性并将光标位置设置到结尾:

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;            
}

Etvoilà自动过滤,然后显示漂亮的显示和箭头选择。

编辑2

对于不区分大小写的搜索,您可以使用:

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

注:

下拉列表打开后cursor will disappear。为防止出现这种情况,必须将Cursor.Current设置为Cursor.Defualt

comboBox1.DroppedDown = true;
Cursor.Current = Cursors.Default;

答案 1 :(得分:0)

如果您使用词典作为数据源,则以下内容可能会有用。 我以表单的形式创建了一个静态函数,以便能够重用它,因为我有多个ComboBox实例,我希望对其具有相同的行为。 只需从TextUpdate事件中调用该函数,并传递控件名称和源字典即可。

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; 
}
相关问题