ListView.ListViewItemCollection.Add这个函数调用的时候出错了。
我开了一个线程,每发送一次数据,就在ListView里面显示出来,结果程序运行一段是时间后,就报错,错误如下:System.ArgumentOutOfRangeException:Insertion index was out of range. Must be non-negative and less than or equal to size.
Parameter name:index
at System.Collections.ArrayList.Insert(Int32 index,Object value)
at System.Windows.Forms.ListView.InsertItems(Int32 displayIndex,ListViewItem[] items,Boolean checkHosting)
at System.Windows.Forms.ListView.IistViewNativeItemCollection.Add(ListViewItem value)
at System.Windows.Forms.ListView.IistViewItemCollection.Add(ListViewItem value)是不是我添加的ListViewItem太多了,超过了允许的最大数量,所以出错了?
我开了一个线程,每发送一次数据,就在ListView里面显示出来,结果程序运行一段是时间后,就报错,错误如下:System.ArgumentOutOfRangeException:Insertion index was out of range. Must be non-negative and less than or equal to size.
Parameter name:index
at System.Collections.ArrayList.Insert(Int32 index,Object value)
at System.Windows.Forms.ListView.InsertItems(Int32 displayIndex,ListViewItem[] items,Boolean checkHosting)
at System.Windows.Forms.ListView.IistViewNativeItemCollection.Add(ListViewItem value)
at System.Windows.Forms.ListView.IistViewItemCollection.Add(ListViewItem value)是不是我添加的ListViewItem太多了,超过了允许的最大数量,所以出错了?
Add根本不需要index的,所以应该不会出现负数。
我的函数用的是这个。
是不是他自己内部调用了insert函数?
System.Windows.Forms.ListViewItem view1 =new ListViewItem(SomeTypes.displaystring(this.CForm.NO, System.DateTime.Now.ToString(), 0, s));
this.CForm.NO++;
this.CForm.ItemAdd(view1);
public void ItemAdd(ListViewItem view1)
{
if (this.listViewBytes.InvokeRequired)
{
SetListViewItem AddItem = new SetListViewItem(ItemAdd);
this.listViewBytes.Invoke(AddItem, new object[] { view1 });
}
else
{
this.listViewBytes.Items.Add(view1);
}
}
比如add的时候加上SuspendLayout()和ResumeLayout()
你使用lock一下你的函数
没有人知道这个答案吗?
System.ArgumentOutOfRangeException:Insertion index was out of range. Must be non-negative and less than or equal to size.
Parameter name:index
at System.Collections.ArrayList.Insert(Int32 index,Object value)
at System.Windows.Forms.ListView.InsertItems(Int32 displayIndex,ListViewItem[] items,Boolean checkHosting)
at System.Windows.Forms.ListView.IistViewNativeItemCollection.Add(ListViewItem value)
at System.Windows.Forms.ListView.IistViewItemCollection.Add(ListViewItem value)难道Add里面有调用Insert??
我记得以前测试的使用
用一个Timer 不断添加数据
好像是1000吧就奔溃了
以前遇到过类似的问题 ListView 控件用Add函数的时候,如果集为空会自动new一个
如果用Insert的话,则不会自动new