【问题标题】:ComboBox searching in string, not just the first letterComboBox 在字符串中搜索,而不仅仅是第一个字母
【发布时间】:2015-11-29 19:39:40
【问题描述】:

我在组合框内搜索项目中的字符串时遇到问题。 我想缩小成员名单。它们以这种方式格式化(唯一的成员 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;
    }

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

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

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

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

感谢您花时间查看。

【问题讨论】:

  • 这个“将第一项填充到文本字段中”,我会说这是默认行为。每当我第一次将某些内容添加到组合框中时,它也会发生在我身上,无论它是一个还是多个。默认情况下,当您第一次通过数据源或仅通过普通的 comboBox.Items.Add(stringhere) 向组合框动态添加某些内容时,它会发生。
  • 好的,我想知道使用 ComboBox.Items.RemoveAt 对字符串进行排序是否会更好(知道在设置数据源时不能这样做),但我认为我会更容易使用数据源“重置”列表。我寻找一个属性来删除最初的“填充”,但似乎找不到任何东西。感谢您的输入:)
  • 我建议您根据您在评论中提出的问题更新您的问题,以便其他人可以直接回答:)

标签: c# string search combobox


【解决方案1】:

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

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

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

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

这是我的解决方法,如果你有更好的方法,我会全力以赴:)

【讨论】:

    猜你喜欢
    • 2016-03-31
    • 2015-03-03
    • 2021-05-11
    • 2012-11-20
    • 1970-01-01
    • 1970-01-01
    • 2020-05-06
    • 2022-11-15
    • 2018-02-08
    相关资源
    最近更新 更多