簡體   English   中英

ComboBox在字符串中搜索,而不僅僅是第一個字母

[英]ComboBox searching in string, not just the first letter

我有問題使我的組合框搜索項目中的字符串。 我想縮小一份成員名單。 它們以這種方式格式化(唯一成員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;
    }

我試過的一些代碼似乎過濾了列表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

謝謝你花時間看。

好吧,好像我自己想出了一些東西,不知道它是否是最好的方式,但似乎完成了工作:)

首先,我將字符串添加到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事件處理程序,它將在消息框中顯示大多數信息(如果不是全部)。

這是我的解決方法,如果你有更好的方法,我都是耳朵:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM