如何在 vb.net 中获取 ListView 的 SelectedItem 或 SelectedIndex

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

How do i get the SelectedItem or SelectedIndex of ListView in vb.net

vb.netwinformslistview

提问by Shahrukh

As you know by question that what I want. I was using listbox. In ListBoxwe can get selected item by a simple line of code: listbox1.SelectedItem. Now I am using ListView, how I get the SelectedItemor SelectedIndexof ListView.

正如你所知道的,我想要什么。我正在使用列表框。在ListBox我们可以通过一个简单的代码行获得所选项目: listbox1.SelectedItem。现在,我使用ListView,我如何获得SelectedItemSelectedIndexListView

回答by Olivier Jacot-Descombes

ListViewreturns collections of selected items and indices through the SelectedItemsand SelectedIndicesproperties. Note that these collections are empty, if no item is currently selected (lst.SelectedItems.Count = 0). The first item that is selected is lst.SelectedItems(0). The index of this item in the Itemscollection is lst.SelectedIndices(0). So basically

ListView通过SelectedItemsSelectedIndices属性返回选定项目和索引的集合。请注意,如果当前未选择任何项目,则这些集合为空 ( lst.SelectedItems.Count = 0)。选择的第一个项目是lst.SelectedItems(0)。该项目在Items集合中的索引是lst.SelectedIndices(0)。所以基本上

lst.SelectedItems(0)

is the same as

是相同的

lst.Items(lst.SelectedIndices(0))

You can also use check boxes. Set CheckBoxesto Truefor this. Through the CheckedItemsand CheckedIndicesproperties you can see which items are checked.

您还可以使用复选框。设置CheckBoxesTrue这一点。通过CheckedItemsCheckedIndices属性,您可以查看检查了哪些项目。

回答by Shahrukh

Here's the answer that I found for my question:

这是我为我的问题找到的答案:

urlList1.FocusedItem.Index

And I am getting selected item value by:

我通过以下方式获取选定项目的价值:

urlList1.Items(urlList1.FocusedItem.Index).SubItems(0).Text

回答by Raphael Estrada

VB6:Listview1.selecteditem

VB6:Listview1.selecteditem

VB10:Listview1.FocusedItem.Text

VB10:Listview1.FocusedItem.Text

回答by Kaya Kipgen

Private Sub ListView1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListView1.Click
        Dim tt As String
        tt = ListView1.SelectedItems.Item(0).SubItems(1).Text
        TextBox1.Text = tt.ToString
End Sub

回答by ismail osunlana

If you want to select the same item in a listboxusing a listview, you can use:

如果要listbox使用 a在 a 中选择相同的项目listview,可以使用:

Private Sub ListView1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged
    For aa As Integer = 0 To ListView1.SelectedItems.Count - 1
        ListBox1.SelectedIndex = ListView1.SelectedIndices(aa)
    Next
End Sub

回答by Chandra Malla

ListView.FocusedItem.Index 

or you can use foreach loop like this

或者你可以像这样使用 foreach 循环

int index= -1;
foreach (ListViewItem itm in listView1.SelectedItems)
{
    if (itm.Selected)
    {
        index= itm.Index;
    }
}

回答by Narinder Rattu

Please Try This for Getting column Index

请尝试此获取列索引

Private Sub lvDetail_MouseMove(sender As Object, e As MouseEventArgs) Handles lvDetail.MouseClick

    Dim info As ListViewHitTestInfo = lvDetail.HitTest(e.X, e.Y)
    Dim rowIndex As Integer = lvDetail.FocusedItem.Index
    lvDetail.Items(rowIndex).Selected = True
    Dim xTxt = info.SubItem.Text
    For i = 0 To lvDetail.Columns.Count - 1
        If lvDetail.SelectedItems(0).SubItems(i).Text = xTxt Then
            MsgBox(i)
        End If
    Next
End Sub