The ItemSelectionChanged event is called by clicking the ListView item to delete the item.
The Code is as follows:
private void listView1_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e) { listView1.Items.Remove(e.Item); }
Exception:
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)
When compiled into EXE, the following error occurs:
This is because, when you click the first item, it enters the event. After the first item is deleted, it enters the event again. At this time, e is still the value of the first item, so an error is returned. Index overflow. The solution is to perform this operation in the Click event.
private void listView1_Click(object sender, EventArgs e) { if (listView1.SelectedItems.Count > 0) { listView1.Items.Remove(listView1.SelectedItems[0]); } }