views:

236

answers:

2

I have a ListView control set up in details mode, and on a button press I would like to retrieve all column values from that row in the ListView.

This is my code so far:

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    Dim items As ListView.SelectedListViewItemCollection = _
    Me.ManageList.SelectedItems
    Dim item As ListViewItem
    Dim values(0 To 4) As String
    Dim i As Integer = 0
    For Each item In items
        values(i) = item.SubItems(1).Text
        i = i + 1
    Next
End Sub

But values just comes out as an empty array. Any ideas? I just want the values array to be filled with the data of that ListView row.

Cheers.

A: 

Just to check, are you aware that item.SubItems(1).Text will get the texts from the second column? And that since you're using SelectedItems it'll only look at the currently selected items in the ListView.

Edit: Also, are you sure there will always just be a maximum of 5 selected items in the ListView? Otherwise you'll have problems with

Dim values(0 To 4) As String
ho1
Yeah, I'm aware it's 0 based and looking at the selected items is what I want :)
Andrew
Then I can't think of anyhting more since the code above works (for up to 5 selections).
ho1
You don't have any hidden (0 width) columns so that you might be reading the wrong one?
ho1
If I try: `MessageBox.Show(values(2))` or something the messagebox comes up with no text, making me think the array is empty. But yeah, the code does compile and run fine.
Andrew
And nah I don't have any hidden columns.
Andrew
Rather than using messagebox, put a breakpoint on the End Sub line and debug it and when it breaks you can move the mouse over the values variable and click on the little + sign to see all the values of it.
ho1
+1  A: 

You need to iterate the SubItems, not the selected items. Fix:

If Me.ManageList.Items.Count <> 1 Then Exit Sub
Dim row As ListViewItem = Me.ManageList.Items(0)
Dim values(0 To row.SubItems.Count-1) As String
Dim i As Integer = 0
For Each item As ListViewItem.ListViewSubItem In row.SubItems
  values(i) = item.Text
  i = i + 1
Next
Hans Passant