ComboBox在字符串中搜索,而不仅仅是第一个字母

Nic*_*sen 5 c# string search combobox

我有问题使我的组合框搜索项目中的字符串.我想缩小一份成员名单.它们以这种方式格式化(唯一成员ID) - 名字 - 姓氏.

当我按"原样"保留所有设置时,它只会"允许"我搜索字符串中的第一个字符.

DataSource是从列表中设置的,循环遍历文件夹中的所有文件.

我一直在使用的代码如下(部分代码)

    private void searchForShooterComboBox_KeyUp(object sender, KeyEventArgs e)
    {
        //if(e => KeyCode == Keys::Down || e => KeyCode == Keys::Down)
        //string comboBoxValue = searchForShooterComboBox.Text;
        //searchForShooterComboBox.DataSource = null;
        //searchForShooterComboBox.DataSource = fliterComboBox(searchForShooterComboBox, memberFileNames);
        //searchForShooterComboBox.Text = comboBoxValue;
    }

    private void searchForShooterComboBox_TextChanged(object sender, EventArgs e)
    {
        searchForShooterComboBox.DataSource = null;
        searchForShooterComboBox.DataSource = fliterComboBox(searchForShooterComboBox, memberFileNames);
    }
private List<string> fliterComboBox(ComboBox cobx, List<string> stringList)
    {
        List<string> returnList = new List<string>();

        if (cobx.Text != ""){
            try
            {
                foreach (string s in stringList)
                {
                    if (s.Contains(cobx.Text))
                    {
                        returnList.Add(s);
                    }
                }
            }catch{
            }
        }
        return returnList;
    }
Run Code Online (Sandbox Code Playgroud)

我试过的一些代码似乎过滤了列表OK,但是在方法运行之后它将新列表中的第一项填充到"文本字段"中,因此用户将无法继续键入名称ex .

使用ComboBox.Items.Add()ComboBox.Items.Remove()不是使用它会有什么不同DataSource吗?

编辑:comboBox DataSource最初在form_load事件处理程序中设置.以下关于组合框的代码是:

searchForShooterComboBox.DropDownStyle = ComboBoxStyle.DropDown;
searchForShooterComboBox.AutoCompleteMode = AutoCompleteMode.Suggest;
searchForShooterComboBox.AutoCompleteSource = AutoCompleteSource.ListItems
Run Code Online (Sandbox Code Playgroud)

谢谢你花时间看.

Nic*_*sen 0

好吧,看来我自己想出了一些办法,不知道这是否是最好的方法,但似乎完成了工作:)

\n\n

首先,我将字符串添加到 和ComboBox.itemslist<string>。以两种方式添加它们的原因是为了让用户在加载时看到所有可用选项。

\n\n
            for (int i = 0; i < membersFiles.Length; i++)\n        {\n            searchForShooterComboBox.Items.Add(membersFiles[i].Replace(".txt", "").Replace(@"C:\\Users\\Nicolai\\Desktop\\skytter\\", "").Replace("-", " "));\n            memberFileNames.Add(membersFiles[i].Replace(".txt", "").Replace(@"C:\\Users\\Nicolai\\Desktop\\skytter\\", "").Replace("-", " "));\n        }\n
Run Code Online (Sandbox Code Playgroud)\n\n

之后,我从属性窗口添加了一个combobox_keydown事件。

\n\n
private void searchForShooterComboBox_KeyDown(object sender, KeyEventArgs e)\n    {\n        try\n        {\n            //checking if the key pressed is RETURN, in that case try to fill the combobox with the selected item,\n            //and continuing with other method\n            if (e.KeyValue == 13)\n            {\n                searchForShooterComboBox.Text = (string)searchForShooterComboBox.SelectedItem;\n                fillInfoInForm();\n            }\n            //making sure the key pressed IS NOT DOWN, UP, LEFT, RIGHT arrow key.\n            else if (e.KeyValue > 40 || e.KeyValue < 37)\n            {\n                filterComboBox(searchForShooterComboBox, searchForShooterComboBox.Text);\n                searchForShooterComboBox.Select(searchForShooterComboBox.Text.Length, 0);\n                searchForShooterComboBox.DroppedDown = true;\n            }\n        }\n        catch (FileNotFoundException ex) {\n            MessageBox.Show("Der blev ikke fundet nogen fil med flg. sti " + ex.FileName + "\\nHusk at v\xc3\xa6lge hele navnet i listen, eller skriv det n\xc3\xb8jagtigt som det st\xc3\xa5r!");\n        }\n    }\n
Run Code Online (Sandbox Code Playgroud)\n\n

使用此方法搜索列表项,清除组合框中的项目,然后添加匹配的项目。

\n\n
    private void filterComboBox(ComboBox cobx, string enteredSearch)\n    {\n        //clearing ComboBox items before adding the items from the LIST that meets the search\n        cobx.Items.Clear();\n\n        //looping over the items from the list, comparing them to the search from the combobox text field.\n        //if the item in the list does not contain the string searched it will return an index of -1.\n        for (int i = memberFileNames.Count-1; i >= 0; i--)\n        {\n            if (memberFileNames[i].IndexOf(enteredSearch, 0, StringComparison.CurrentCultureIgnoreCase) >= 0)\n            {\n                cobx.Items.Add(memberFileNames[i]);\n            }\n        }\n    }\n
Run Code Online (Sandbox Code Playgroud)\n\n

如果您无法找到正确的 KeyValue,请尝试查看 \n https://msdn.microsoft.com/en-us/library/system.windows.forms.keyeventargs.keyvalue(v=vs.110).aspx \ nand 从那里复制粘贴代码,并将其添加到您的 key_down 事件处理程序中,它将在消息框中显示大部分信息(如果不是全部)。

\n\n

这是我的解决方法,如果你有更好的方法,我洗耳恭听:)

\n