1 Replies - 143 Views - Last Post: 08 February 2012 - 04:52 AM Rate Topic: -----

Topic Sponsor:

#1 goldthelocks  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 13-December 10

Getting selected rows from listview always throws out error

Posted 08 February 2012 - 04:21 AM

Hello everyone!

I have a really simple question but unfortunately I can't answer it! I am trying to get the selected rows from a listview. To achieve this, I did this:

Dim numsel As Integer

        numsel = Me.ListView1.Items.Count

        For countme As Integer = 0 To numsel
            If Me.ListView1.Items(countme).Selected = True Then
                For subitem As Integer = 0 To 3
                    MessageBox.Show("hello!" & vbNewLine & Me.ListView1.Items(countme).SubItems(subitem).Text)
                Next
            End If
        Next



It does count the selected rows, but it always throws out the error saying "ArgumentOutOfRange Exception", or something along those lines. For example, if I selected a row, it will output the results of that row. But the loop goes on. I tried everything I can but to no avail. I specifically want to select multiple rows, and I can achieve that if someone can assist me with this simple "problem" I have.

I will be so grateful for anyone who will help me. Have a nice day!

Is This A Good Question/Topic? 0
  • +

Replies To: Getting selected rows from listview always throws out error

#2 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 960
  • View blog
  • Posts: 3,354
  • Joined: 25-September 09

Re: Getting selected rows from listview always throws out error

Posted 08 February 2012 - 04:52 AM

Check out .SelectedItems it is a collection of the items you have selected in a listview.

As far as your error. What is the value of numsel?

Now let's say it's 5

that means that the loops needs to run 5 times but 0 to 5 which is what you have is actually 6 iterations.
Now we know that the index is 0 based so we have to start at 0 so what would you have to do with 5 to only make the number of iterations = 5?

This post has been edited by CharlieMay: 08 February 2012 - 04:56 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1