Я попытался создать фильтр для поля со списком при вводе в него текста, и он отображает раскрывающийся список с соответствующими значениями из списка со списком, который я использовал здесь: C# Добавление фильтра в раскрывающийся список combobox Но я заметил, что когда я набираю символы, у которых нет соответствующих значений из комбинированного списка, а затем нажмите Enter или щелкните на мыши он выдает тип исключения System.ArgumentOutOfRangeException
и говорит InvalidArgument=Value, это мой код:
private void comboBox1_TextUpdate(object sender, EventArgs e)
{
try
{
comboBox1.DroppedDown = true;
Cursor.Current = Cursors.Default;
string filter_param = comboBox1.Text;
string[] filteredItems = Array.FindAll(arrProjectList, c => c.Contains(filter_param)); //'arrProjectList' is a global string array that holds and fills the combobox values
// another variant for filtering using StartsWith:
//List<string> filteredItems = arrProjectList.FindAll(x => x.StartsWith(filter_param));
comboBox1.DataSource = filteredItems;
if (string.IsNullOrWhiteSpace(filter_param))
{
comboBox1.DataSource = filteredItems;
}
comboBox1.DroppedDown = true;
// this will ensure that the drop down is as long as the list
comboBox1.IntegralHeight = true;
// remove automatically selected first item
comboBox1.SelectedIndex = -1;
comboBox1.Text = filter_param;
// set the position of the cursor
comboBox1.SelectionStart = filter_param.Length;
comboBox1.SelectionLength = 0;
//comboBox1.AutoCompleteSource = AutoCompleteSource.ListItems;
//comboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
}
catch
{
MessageBox.Show("error");
}
}
Я не нашел решения для этого в руководстве. Как это можно исправить?
comboBox1.DataSource = filteredItems;
, поэтому оно будет выполняться только в том случае, если:if(filteredItems.Length > 0)
и теперь оно работает нормально. - person Black-Wolf   schedule 09.09.2018