如何遍历Listview项目

use*_*468 1 vb.net listview visual-studio-2010 visual-studio

我认为我需要某种循环,以将变量包含在access db中。我现在遇到的问题是,使用我正在使用的代码,它将获得第一个值,如果我单击另一个项目,它将保留旧值,而不会使用新值更新。

如何创建一个循环,该循环将存储所选项目中的值。谢谢

           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
Run Code Online (Sandbox Code Playgroud)

Ňɏs*_*arp 5

The code I supplied just gets the first value 因为您只看了一遍又一遍:

 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 
Run Code Online (Sandbox Code Playgroud)

这将处理lvselecteditems名称非常混乱的所有项目。要仅处理所选项目,请使用

For Each item As ListViewItem In Me.lvSelectedItems.SelectedItems 
Run Code Online (Sandbox Code Playgroud)