实现combobox模糊查询的时候报错 InvalidArgument=“0”的值对于“index”无效
因为要对combobox实现模糊查询,因为系统实现的匹配只能从左到右进行匹配,所以利用两个list来进行模糊匹配,主要代码如下:
List<string> listOnit = new List<string>(); //绑定原始数据源
List<string> listNew = new List<string>();
private void comboBox1_TextUpdate(object sender, EventArgs e)
{
this.comboBox1.Items.Clear();
listNew.Clear();
//在原始数据源中遍历,把包含当前输入的内容添加到listNew中
foreach (string item in listOnit)
{
if (item.Contains(this.comboBox1.Text))
{
listNew.Add(item);
}
}
this.comboBox1.Items.AddRange(listNew.ToArray());
this.comboBox1.SelectionStart = this.comboBox1.Text.Length;
Cursor = Cursors.Default;
this.comboBox1.DroppedDown = true;
}
这个代码可以实现模糊匹配,但是有个诡异的错误,就是当你输入某些内容先匹配到,但是最后却没有匹配项就会报错。打个比方,你想输入“张四”,数据源中有“张三”,你先输入“张”,然后出现所有带有“张”的匹配项,然后你在输入“四”,则没有了匹配项,则会出现报错,报错信息如下:
************** 异常文本 **************
System.ArgumentOutOfRangeException: InvalidArgument=“0”的值对于“index”无效。
参数名: index
在 System.Windows.Forms.ComboBox.ObjectCollection.get_Item(Int32 index)
在 System.Windows.Forms.ComboBox.get_Text()
在 System.Windows.Forms.ComboBox.WmReflectCommand(Message& m)
在 System.Windows.Forms.ComboBox.WndProc(Message& m)
在 System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
我在网上找,一直找不到原因,我也找不到这个问题具体错在什么地方。要吐槽的是这个Textupdate事件根本没办法调试,因为当你输入第一个字匹配之后,他就会默认选择第一项从而进入selectchange事件,也就没办法调试了,后来苦苦思索终于想到一个解决方法,虽然不知道原因,但终究还是解决了。就是当combobox没有匹配项的时候,就在它的下拉框添加空字符串,这样就能组织报错了,稍微改了一下代码就可以了。
private void comboBox1_TextUpdate(object sender, EventArgs e)
{
this.comboBox1.Items.Clear();
listNew.Clear();
foreach (string item in listOnit)
{
if (item.Contains(this.comboBox1.Text))
{
listNew.Add(item);
}
}
if (listNew.Count != 0)
{
this.comboBox1.Items.AddRange(listNew.ToArray());
this.comboBox1.SelectionStart = this.comboBox1.Text.Length;
Cursor = Cursors.Default;
this.comboBox1.DroppedDown = true;
}
else
{
this.comboBox1.Items.Add("");
this.comboBox1.SelectionStart = this.comboBox1.Text.Length;
}
}
实现combobox模糊查询的时候报错 InvalidArgument=“0”的值对于“index”无效的更多相关文章
- Winform如何实现ComboBox模糊查询
最近朋友问了一个关于Winform实现ComboBox模糊查询的知识点,自己好久没有搞Winform了,就上手练了一下,废话不多说,进入正题. 前台设计: 前台就是一个简单的Form窗体+一个Comb ...
- Mybatis传多个参数的问题 及MyBatis报错 Parameter '0' not found. Available parameters are [arg1, arg0, param1 问题
对于使用Mybatis ,传多个参数,我们可以使用对象封装外,还可以直接传递参数 对象的封装,例如查询对象条件basequery对象 <select id="getProductByP ...
- LoadXml 加载XML时,报错:“根级别上的数据无效。 行1,位置1“
==XML=================================== <?xml version="1.0" encoding="utf-8" ...
- Python2.7在Windows下CMD编码为65001/utf-8时print报错[Errno 0]/[Errno 2]
使用python2.7处理unicode的字符串,环境变量已设置PYTHONIOENCODING为utf-8,cmd编码为utf-8时print unicode字符串会报错[Errno 0]或[Err ...
- Ubuntu系统---报错Assertion '0' failed
Ubuntu系统---报错Assertion '0' failed YOLO V3,CUDA Error: out of memory darknet: ./src/cuda.c:36: check_ ...
- SAP 对HU做货物移动报错-Only 0 serial numbers entered instead of 30 -
SAP 对HU做货物移动报错-Only 0 serial numbers entered instead of 30 - 元旦刚过,就收到客户的业务人员报错说,当其对HU做转库(同一个公司代码下工厂到 ...
- 【EasyUI】combotree和combobox模糊查询
这里说的模糊查询指在输入框输入,然后自动在下拉框中显示匹配结果,类似Google搜索提示 EasyUI库已经实现了combobox的查询过滤功能,但只能从头匹配,原因是EasyUI库的代码限制: fi ...
- easyui combobox模糊查询
用easyui框架开发的攻城狮恐怕都遇到过这样一个问题,就是在新增页面combobox下拉框需要支持模糊查询,但是输入不是combobox中Data里面的值的时候,点击保存,依然是可以新增进去的,这样 ...
- 十六:SQL注入之查询方式及报错盲注
在很多注入时,有很多注入会出现无回显的情况,其中不回显的原因可能是SQL查询语句有问题,这时候我们需要用到相关的报错或者盲注进行后续操作,同时作为手工注入的时候,需要提前了解SQL语句能更好的选择对应 ...
随机推荐
- 动态规划—distinct-subsequences
题目: Given a string S and a string T, count the number of distinct subsequences of T in S. A subseque ...
- hdu 4625 Dice(概率DP)
Dice Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Submi ...
- python3.7爬取墨菲定律保存在本地txt
#!/usr/local/bin/python3.7 # -*- coding: utf-8 -*- # @Time: 2019/07/15 # @Function 获取在线文本内容 import r ...
- Flutter-Boxdecoration邊框線, 圓角
decoration: BoxDecoration( borderRadius: BorderRadius.circular(8), border: Border( top: BorderSide( ...
- html 头部设置
https://juejin.im/post/5a4ae29b6fb9a04504083cac <head> <meta charset="UTF-8"> ...
- c++11 快速退出
C++程序中有以下几种推出方式:terminate abort exit terminate: 实际上是c++语言中异常处理的一部分(包含在<exception>头文件中),一般而言,没有 ...
- .Net Core 学习目录(搁置)
简介 .NET Core 是.NET Framework的新一代版本,是微软开发的第一个官方版本,具有跨平台 (Windows.Mac OSX.Linux) 能力的应用程序开发框架 (Applicat ...
- String Problem
String Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- java logger的info记录在哪
这个要看你的日志配置文件是怎么配置的,以log4j为例log4j.appender.mainLog=org.apache.log4j.ConsoleAppenderlog4j.appender.mai ...
- MySQL常用SQL(含复杂SQL查询)
1.复杂SQL查询 1.1.单表查询 (1)选择指定的列 [例]查询全体学生的学号和姓名 select Sno as 学号,Sname as 姓名 from student; select Sno,S ...