Im programming in WindowsForms, trying to Remove items from a listview using a button. I tried with the following code:
private void btnQuitar_Click(object sender, EventArgs e)
{
if(listvPrincipal.Items.Count > 0)
{
while (listvPrincipal.Items.Count > 0)
listvPrincipal.Items.Remove(listvPrincipal.SelectedItems[0]);
}
}
But every time i select an item and click the "Remove" button, the following error occurs:
InvalidArgument = Value of '0' is not valid for 'index'.
I searched for this problem but every answer said the problem was that no item was selected and to solve it you should make sure " Items.Count > 0 ".
Before i press the button i make sure i have an item selected.
I have used the same code in another project and it works just fine. Im not sure why it doesn't work here. Any idea?