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

5
我有问题在我的下拉框中搜索项目中的字符串。我想缩小成员列表。它们以这种方式格式化(唯一成员ID) - 名字 - 姓氏。
当我将所有设置保持“不变”时,它只允许我在字符串的第一个字符中搜索。
数据源从列表中设置,该列表是从循环遍历文件夹中的所有文件生成的。
我一直在使用以下代码(部分代码):
    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;
    }

我尝试过的一些代码似乎可以很好地过滤列表,但在方法运行后,它会将新列表中似乎是第一个项目填充到“文本字段”中,所以用户将无法继续输入名称。

使用ComboBox.Items.Add()ComboBox.Items.Remove()是否会有任何区别,而不是使用DataSource

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

searchForShooterComboBox.DropDownStyle = ComboBoxStyle.DropDown;
searchForShooterComboBox.AutoCompleteMode = AutoCompleteMode.Suggest;
searchForShooterComboBox.AutoCompleteSource = AutoCompleteSource.ListItems

感谢您抽出时间查看。

这个“将第一个项目填入文本字段”的行为,我认为它是默认行为。无论我第一次向组合框中添加一个或多个项目,都会发生这种情况。当您首次通过数据源或仅通过comboBox.Items.Add(stringhere)动态向组合框添加内容时,默认情况下会发生这种情况。 - Kaitlyn
好的,我在想是否最好使用 ComboBox.Items.RemoveAt 对字符串进行排序(知道当数据源设置时无法执行此操作),但认为使用数据源“重置”列表会更容易。我寻找了一个删除初始“填充”的属性,但似乎找不到。感谢您的建议 :) - Nicolai Svendsen
我建议您更新您的问题并根据评论中的内容进行提问,这样其他人可以直接回答 :) - Kaitlyn
1个回答

0

好的,看起来我自己解决了一些问题,不知道这是否是最好的方法,但似乎能够完成工作 :)

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

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

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

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

创建了这个方法来搜索列表项,清除下拉框中的项目,并添加匹配的项目。
    private void filterComboBox(ComboBox cobx, string enteredSearch)
    {
        //clearing ComboBox items before adding the items from the LIST that meets the search
        cobx.Items.Clear();

        //looping over the items from the list, comparing them to the search from the combobox text field.
        //if the item in the list does not contain the string searched it will return an index of -1.
        for (int i = memberFileNames.Count-1; i >= 0; i--)
        {
            if (memberFileNames[i].IndexOf(enteredSearch, 0, StringComparison.CurrentCultureIgnoreCase) >= 0)
            {
                cobx.Items.Add(memberFileNames[i]);
            }
        }
    }

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

这是我的解决方法,如果您有更好的方法,请告诉我 :)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接