ArgumentOutOfRangeException for ListViewItem when clicking 2nd time

前端 未结 1 503
悲哀的现实
悲哀的现实 2021-01-28 01:29

The method below when I click second time gives the ArgumentOutOfRangeException error, and says index 0 is not a valid index.

In first click it works!?

ListView

相关标签:
1条回答
  • 2021-01-28 02:09

    You are getting an error because your first if statement will never evalute to true (and return) since count will never go below 0. Due to this, even if your list is empty it still tries to delete the first element, throwing an ArgumentOutOfRangeException.

    Your if statement should instead check if it is equal to 0, as such:

     if (li.SelectedItems.Count == 0)
    

    Nothing happens when you press anything except Alt/Control because you are not handling the event for any key press other than those,

     Control.ModifierKeys == Keys.None
    

    Means that no keys are pressed as opposed to anything except Alt/Control being pressed.

    0 讨论(0)
提交回复
热议问题