vb.net 如何遍历 Listview 项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21137895/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 17:23:28  来源:igfitidea点击:

How to Loop through Listview items

vb.netvisual-studio-2010visual-studiolistview

提问by user1532468

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

回答by ??ssa P?ngj?rdenlarp

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

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 

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

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

For Each item As ListViewItem In Me.lvSelectedItems.SelectedItems