vb.net 在 SelectedIndexChanged 上从 Listview 检索项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19997831/
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-17 15:50:03  来源:igfitidea点击:

vb.net retrieve items from Listview on SelectedIndexChanged

vb.netvisual-studio-2010

提问by user1532468

I am trying to get the value from a listview in vb.net, but keep getting an error of:

我试图从 vb.net 中的列表视图中获取值,但不断收到以下错误:

'SelectedItem' is not a member of 'System.Windows.Forms.ListView'.

“SelectedItem”不是“System.Windows.Forms.ListView”的成员。

I think I need to change eventargs to some other event type, but am struggling with this. Could someone please point out my error. Thanks

我想我需要将 eventargs 更改为其他一些事件类型,但我正在为此苦苦挣扎。有人可以指出我的错误。谢谢

Sub filllistview()
        Try
            'creatconn()
            cn.Open()
            Dim cmd As OleDbCommand = New OleDbCommand("Select * from Customers", cn)
            dr = cmd.ExecuteReader()
            While dr.Read()
                ListView1.Items.Add(dr(0).ToString())
                ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(dr(1))

            End While

        Catch ex As Exception
            System.Windows.Forms.MessageBox.Show(ex.Message)
        Finally
            dr.Close()
            cn.Close()
        End Try
    End Sub

Need to catch here

需要抓住这里

Private Sub ListView1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As EventArgs) Handles ListView1.SelectedIndexChanged
        Try
            showcontectsinlistview()
            str = ListView1.SelectedItem **<--- ERROR**

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

回答by Stefano Bafaro

This is true beacuse it doesn't exist a "SelectedItem" property. The ListView object has a property "SelectedItems", that is a collection. So you could use something like: listView1.SelectedItems[0].

这是真的,因为它不存在“SelectedItem”属性。ListView 对象有一个属性“SelectedItems”,即一个集合。所以你可以使用类似的东西:listView1.SelectedItems[0].

With this you will have the first of the selected items in the collection given by the "SelectedItems" property. To navigate through all the selected items, you can loop between them in this way:

有了这个,您将拥有“SelectedItems”属性给出的集合中的第一个选定项目。要浏览所有选定的项目,您可以通过以下方式在它们之间循环:

For Each itm As ListViewItem In ListView1.SelectedItems
If itm.Selected Then
    For i As Integer = 0 To itm.SubItems.Count - 1
        str += itm.SubItems(i).Text
    Next
End If
Next

In this way you build a string with all the values of all the selected items. If you have only 1 selected item in the listview, you will have only that value.

通过这种方式,您可以使用所有选定项目的所有值构建一个字符串。如果您在列表视图中只有 1 个选定项目,则您将只有该值。

回答by user2125649

Look at this example

看这个例子

 Private Sub lstDirectoryInfo_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstDirectoryInfo.SelectedIndexChanged
    Dim str As String = String.Empty

    For Each itm As ListViewItem In lstDirectoryInfo.SelectedItems
        If itm.Selected Then
            For i As Integer = 0 To itm.SubItems.Count - 1
                str = itm.SubItems(i).Text
            Next
        End If
    Next
    MessageBox.Show(str)
End Sub