ComboBox search in a string, not just the first letter

I have a problem with my combobox doing a search inside strings in elements. I want to narrow the list of participants. They are formatted in this way (unique identifier of the participant) - First name - last name.

When I leave all the settings “as is”, it will “allow” me to search in the first char in the string.

The DataSource is installed from the list, which is done from the loop through all the files in the folder.

The code I used is as follows (partial code)

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

some code that I tried, apparently, was filtering the OK list, but after running the methods, it fills what appears to be the first element in the new list into a “text box”, so the user will not be able to continue to type ex.

ComboBox.Items.Add() ComboBox.Items.Remove() DataSource?

edit: comboBox form_load. , combobox:

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

, .

+4
1

, , - , , , , , :)

-, ComboBox.items, list<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, ( ) .

, , :)

0

Source: https://habr.com/ru/post/1605828/


All Articles