我在向组合框输入文字时尝试构建一个组合框过滤器,它显示下拉列表以及组合列表中的匹配值
我从这里使用了指南:C# Adding Filter to combobox dropdown list
但是我注意到,当我键入组合列表中没有匹配值的字符,然后按Enter或单击鼠标时,它会抛出Exception类型的
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");
}
}
我没有在指南中找到解决方案。如何解决?