user1532468
user1532468

Reputation: 1753

How to Loop through Listview items

I need some kind of loop, I think, to put into a variable for inclusion in access db. The problem I have at the minute is that with the code I am using, it gets the first value and if I click on another item, it retains the old value and dosen't update with new value.

How can I create a loop that will store the values from selected items. Thanks

           With lvSelectedItems.Items

                Dim username As String = .Item(0).Text
                Dim session As String = .Item(0).SubItems.Item(1).Text

                output = username + " : " + session
                MessageBox.Show(output)
            End With

Upvotes: 1

Views: 39472

Answers (1)

The code I supplied just gets the first value because you only looked at one item over and over:

 Dim username As String
 Dim session As String
 For Each item As ListViewItem In Me.lvSelectedItems.Items 
      username = Item.Text 
      session = Item.SubItems.Item(1).Text
      output = username + " : " + session 

      console.WriteLine(output)        ' show results of this loop iteration
  Next 

This will process all the items in lvselecteditems which is a very confusing name. To process just the selected items use

For Each item As ListViewItem In Me.lvSelectedItems.SelectedItems 

Upvotes: 8

Related Questions