Click here to Skip to main content
15,894,460 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am trying to loop back to the beginning of my listview items once I have reached the end. Instead, I keep getting a "System.ArgumentOutOfRangeException: 'InvalidArgument=Value of '21' is not valid for 'index'." error.

here is my code:
Dim previousSelectedIndex As Integer = Me.SelectedItems(0).Index

               If Me.SelectedIndices(0) < 21 Then
                   Me.Items(previousSelectedIndex + 1).Selected = True
               Else

                   Me.Items(previousSelectedIndex).Selected = False
                   Me.Items(0).Selected = True
               End If


What I have tried:

I have tried to flip the statement. I also tried to use an elseif, however, that didn't work either.
Posted
Updated 14-May-18 20:15pm

Fixed my problem. now my selection process starts at the top and when it reaches the end I can press enter and it will bring me back to the top.

Here is the code:

Dim previousSelectedIndex As Integer = Me.SelectedItems(0).Index

               If Me.Items.Item(previousSelectedIndex).Index = Me.Items.Count - 1 Then
                   Me.Items(0).Selected = True
               Else
                   Me.Items(previousSelectedIndex + 1).Selected = True
               End If
 
Share this answer
 
Why is your value hard coded? If this is a loop, you can check if the value has reached the end of your sequence and reset it to zero. Why do you want to loop endlessly?
 
Share this answer
 
Comments
Member 11856456 14-May-18 19:51pm    
Christian, This portion of code will be in a keypress event that is initiated by pressing enter. As of right now, it will go through the items but gets the error mentioned up top once it reaches past the max index number.

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