获取第二列中所选项目的值...返回上一个项目

时间:2013-10-15 01:53:58

标签: vb.net

我知道还有其他这样的问题,但由于某些原因,似乎没有人有这个问题。我正在尝试填充我在加载时创建的2列列表视图。

With lstShipMethods
    .View = View.Details
    .FullRowSelect = True
    .HeaderStyle = ColumnHeaderStyle.None ' set to whatever you need
    .Columns.Clear() ' make sure collumnscollection is empty
    ' Add 3 columns
    .Columns.AddRange(New ColumnHeader() {New ColumnHeader(), New ColumnHeader()})
End With

lstShipMethods.Items.Add(New ListViewItem({"col1data", "col2data1"}))
lstShipMethods.Items.Add(New ListViewItem({"col1data", "col2data2"}))

它填充得很好,但是当我尝试从所选项目列中获取数据时

Private Sub lstShipMethods_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lstShipMethods.SelectedIndexChanged
    Dim val As String = lstShipMethods.FocusedItem.SubItems(1).Text
    MessageBox.Show(val)
End Sub

首次点击后,它将始终显示两个值,即第一行的第二列,第二行将在MessageBox中输出。

1 个答案:

答案 0 :(得分:1)

您可以改为使用ItemSelectionChanged事件。

Private Sub lstShipMethods_SelectedIndexChanged(sender As System.Object, e As ListViewItemSelectionChangedEventArgs) Handles lstShipMethods.ItemSelectionChanged
    If e.IsSelected Then
        Dim val As String = lstShipMethods.FocusedItem.SubItems(1).Text
        MessageBox.Show(val)
    End If
End Sub