2016-12-05 87 views
0

需要一些帮助添加过滤器,以我的ComboBox下拉列表(Windows窗体的Visual Studio 2015年)C#中添加过滤器,以组合框下拉列表

下拉填充按如下:

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周所以可能会混淆一些术语或这种语言等

+0

甚至没有单个评论或答复? –

+0

[WinForms ComboBox DropDown和Autocomplete窗口都出现]的可能重复(http://stackoverflow.com/questions/3064780/winforms-combobox-dropdown-and-autocomplete-window-both-appear) – Lucifer

+1

这不是重复的。我有不同的要求可悲。我不想在我输入时显示新列表。我想将主列表过滤为类型。此解决方案必须搜索组合框内的任何文本,而不仅仅是起始字符。您建议的解决方案包括附加列表,不允许在一个项目中搜索任何字符串。因此,例如,如果项目包含99999 Hello World,我希望能够通过键入项目中的任何单词而不仅仅是99999来显示匹配的行。希望这是有道理的。 –

回答

4

的其他方面,我建议使用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  
} 

EDIT

我发现了一个解决方案用于输入过滤器进入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;    
} 

的Et瞧自动过滤用一个漂亮的之后显示和箭头选择。

EDIT 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; 
+0

酷感谢。我会放弃并报告。 –

+0

太好了。只是通读它。欢呼会让你后来知道。 –

+0

我不得不添加electJobDropdown.SelectedIndex = -1;到初始部分,因为它是用第一个值填充列表而不是空白。因此,它在下一个方法中失败 - 请参阅http://pastebin.com/L8pkB0PG –