【问题标题】:How to Loop through Listview items如何循环遍历 Listview 项目
【发布时间】:2014-01-15 12:49:51
【问题描述】:

我认为,我需要某种循环来放入一个变量以包含在 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

【问题讨论】:

  • Nidzaaaa 我知道如何从列表视图中获取选定的项目,但是如何创建一个循环来获取我选择的所有内容。我提供的代码只是获得第一个值。谢谢
  • 但是?!?使用lvSelectedItems.Items.Count 循环有什么问题?
  • 我什至试过这个,但只显示 1 个结果。我需要获取所有列表视图项目而不是选定项目。谢谢 msg = CStr(lvSelectedItems.Items.Count) If CDbl(msg) > 0 Then 'With lvSelectedItems.Items For Each item As ListViewItem In Me.lvSelectedItems.Items Dim username As String = lvSelectedItems.Items.Item(0).Text Dim session As String = lvSelectedItems.Items.Item(0).SubItems.Item(1).Text output = username + " : " + session Next MessageBox.Show(output) 'End With

标签: vb.net visual-studio-2010 visual-studio listview


【解决方案1】:

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 

这将处理lvselecteditems 中的所有项目,这是一个非常令人困惑的名称。要仅处理选定的项目,请使用

For Each item As ListViewItem In Me.lvSelectedItems.SelectedItems 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-11-22
    • 2023-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多