用ListView实现点击ListView的项删除该项的效果,调用ItemSelectionChanged事件。
代码如下:
private void listView1_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
{
listView1.Items.Remove(e.Item);
}发生异常:
System.ArgumentOutOfRangeException: InvalidArgument=Value of '1' is not valid for 'index'.
Parameter name: index
at System.Windows.Forms.ListView.ListViewItemCollection.get_Item(Int32 index)
at System.Windows.Forms.ListView.WmReflectNotify(Message& m)
at System.Windows.Forms.ListView.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
编译成EXE运行时则会会出现这种错误提示:
private void listView1_Click(object sender, EventArgs e)
{
if (listView1.SelectedItems.Count > 0)
{
listView1.Items.Remove(listView1.SelectedItems[0]);
}
}

在C# Winform应用中使用ListView,尝试通过ItemSelectionChanged事件实现点击列表项删除功能时,遇到错误:InvalidArgument=Value of '1' is not valid for 'index'。该错误通常发生在尝试访问已删除项的索引时。解决方法是确保在删除项之前取消选择,防止引用无效索引。

被折叠的 条评论
为什么被折叠?



