Click here to Skip to main content
15,889,200 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
trying to remove item from a listview of a window form application.
it is so weird that it try to throw an exception each time i do it. And there is the code that i use originally :

exception say: Index was out of range.Must be non-negative and less than the size of the collection. Parameter name:index

C#
private void Remove( )
        {
            
            try
            {
                
                      listView1.Items.RemoveAt(listView1.SelectedItems[0].Index);


            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }


What I have tried:

even though i tried to put in direct index 0 , it still would throw the same exception

C#
ListView.SelectedListViewItemCollection album = this.listView1.SelectedItems;
                if (album.Count > 0)
                    listView1.Items.RemoveAt(0);
Posted
Updated 15-Jan-17 0:40am

Did you inspect the SelectedItems collection to make sure it's not empy?
 
Share this answer
 
yes, i did this , the exception get thrown at **


C#
if (listView1.SelectedItems.Count > 0)
                {
                    listView1.Items.RemoveAt(listView1.SelectedItems[0].Index); **
                    
                    
                }
 
Share this answer
 
Comments
Wendelius 15-Jan-17 6:41am    
When posting comments to solutions, don't post new solutions. Instead, press "Have a Question or Comment?" button.
Taken this is Windows Forms....

In the first example you don't examine if there is a selected index or not so it clearly fails when there is no selection.

If the second example fails with the same exact exception, then perhaps the problem is caused by the location of your code. If you execute the code too early the control may not yet be created. You can investigate the situation from Control.IsHandleCreated Property (System.Windows.Forms)[^]

For example if you call the following code from the form constructor you get the message that the listview isn't created yet. Also if you omit the message and let the code run, you can see that there are three items in the listview so deletion hasn't taken place.

If you call it for example from a button click event, it should work fine
C#
public void RemoveItems(bool omitCheck=false) {

   if (!omitCheck) {
      if (!this.listView1.IsHandleCreated) {
         MessageBox.Show("Listview not fully created yet");
         return;
      }
   }

   this.listView1.MultiSelect = true;

   // Add a few items
   this.listView1.Items.Add("First");
   this.listView1.Items.Add("Second");
   this.listView1.Items.Add("Third");

   // Delete selected items (none)
   foreach (ListViewItem item in this.listView1.SelectedItems) {
      this.listView1.Items.Remove(item);
   }

   // Select two items
   this.listView1.Items[0].Selected = true;
   this.listView1.Items[2].Selected = true;

   // Delete selected items (two)
   foreach (ListViewItem item in this.listView1.SelectedItems) {
      this.listView1.Items.Remove(item);
   }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900