dcsimg
Results 1 to 8 of 8

Thread: [RESOLVED] Listview - Out Of Range Exception

  1. #1

    Thread Starter
    Addicted Member sinner0636's Avatar
    Join Date
    Sep 2009
    Posts
    233

    Resolved [RESOLVED] Listview - Out Of Range Exception

    hello i am trying to loop a listview and remove the items that are checked from the 2nd column that has the files path but keep getting a out of bounds error any ideas? Thanks

    Code:
       Public Function DirExists(ByVal Path As String) As Boolean
            If My.Computer.FileSystem.DirectoryExists(Path) Or My.Computer.FileSystem.FileExists(Path) Then
                Return True
            Else
                Return False
            End If
        End Function
    
        Public Function DeleteFiles(ByVal FileName As String)
            Try
                If DirExists(FileName) Then
                    Application.DoEvents()
                    My.Computer.FileSystem.DeleteFile(FileName)
                End If
            Catch ex As Exception
            End Try
        End Function
    
    
       For i As Integer = 0 To Me.ListView1.Items.Count - 1
                If Me.ListView1.SelectedItems(i).Checked = True Then
    
                    Call DeleteFiles(Me.ListView1.SelectedItems(i).SubItems(2).Text)
                    Me.ListView1.SelectedItems(i).Remove()
                End If
            Next

  2. #2
    Lively Member Goggy's Avatar
    Join Date
    Oct 2017
    Posts
    81

    Re: Listview - Out Of Range Exception

    ListView1.Items and ListView1.SelectedItems, are not the same collections
    Utterly useless, but always willing to help

    As a finishing touch god created the dutch

  3. #3
    PowerPoster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,176

    Re: Listview - Out Of Range Exception

    Try changing ListView1.Items.Count to Listview1.SelectedItems.Count
    Please remember next time...elections matter!

  4. #4

    Thread Starter
    Addicted Member sinner0636's Avatar
    Join Date
    Sep 2009
    Posts
    233

    Re: Listview - Out Of Range Exception

    so got it working started deleting them and removing them from th list then all sudden got a error


    argument out of range exception on this line
    invalidargument=value of 65 in not valid index
    Code:
    If Me.ListView1.Items(i).Checked = True Then

    Code:
       For i As Integer = 0 To Me.ListView1.Items.Count - 1
                If Me.ListView1.Items(i).Checked = True Then
                    Call DeleteFiles(Me.ListView1.Items(i).SubItems(2).Text)
                    Me.ListView1.Items(i).Remove()
                End If
            Next

  5. #5
    PowerPoster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,176

    Re: Listview - Out Of Range Exception

    This is pure guessing but the index may be changing as things are deleted. So if you had 10 the high index is 9. If you delete one the high index is 8. Just guessing...

    If so save Me.ListView1.Items.Count to a variable and loop by that. When you delete; delete one from that count also.
    Last edited by TysonLPrice; Dec 20th, 2017 at 08:57 AM.
    Please remember next time...elections matter!

  6. #6
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    40,350

    Re: Listview - Out Of Range Exception

    I don't think that's a safe way to do it, because the number of items can still reduce and I'm not sure quite how reducing the variable would be treated.

    One way to do it is to loop backwards, eg:
    Code:
       For i As Integer = Me.ListView1.Items.Count - 1 To 0 Step -1
    With this, when an item is removed the loop counter has already dealt with it (and all later ones), so you can't get a problem from it trying to read one it has already removed.

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    32,755

    Re: Listview - Out Of Range Exception

    I'm surprised that you could do that at all. Looping backwards is the only reasonable way to go. The number of iterations for the loop is obtained the first time the loop is executed. So, the value of Me.ListView1.Items.Count - 1 is evaluated only the first time. Suppose it were 10. You then delete an item, so the number of items is now just 9, and you are looking at the second one, not the first, so you'll run right off the end. Looping backwards, as Si showed, solves that problem.
    My usual boring signature: Nothing

  8. #8

    Thread Starter
    Addicted Member sinner0636's Avatar
    Join Date
    Sep 2009
    Posts
    233

    Re: Listview - Out Of Range Exception

    Thanks for the replys Si Geek that worked

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width