VB.NET:如何动态选择列表视图项?

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

VB.NET: How to dynamically select a list view item?

vb.netlistviewdynamicselection

提问by redned

I need to dynamically select an item in a listview based on what was selected previously.

我需要根据之前选择的内容在列表视图中动态选择一个项目。

The items that have been selected in the past are retrieved from a database and added to an Arraylist. These items then need to be selected from a number of different listviews.

从数据库中检索过去选择的项目并将其添加到 Arraylist。然后需要从许多不同的列表视图中选择这些项目。

Doing this by index like so listRef1.Items(2).Checked = Trueis no problem but I need to do it by the item text, i.e. one of the strings in the array.

像这样按索引执行此listRef1.Items(2).Checked = True操作没问题,但我需要按项目文本执行此操作,即数组中的字符串之一。

So far I have this:

到目前为止,我有这个:

For i As Integer = 0 To refsArr.Count - 1
   'find the correct category id
    Dim cmdRefCat As New SqlCommand("SELECT RefID from ReferencesListTable WHERE            RefName = '" & refsArr(i) & "'", conn)
    Dim refid As Integer = cmdRefCat.ExecuteScalar()
    If refid = 1 Then
        listRef1.Items(refsArr(i)).Checked = True
    ElseIf refid = 2 Then
        listRef2.Items(refsArr(i)).Selected = True
        listRef2.Select()
    ElseIf refid = 3 Then
        listRef3.Items.Item(refsArr(i)).Selected = True
        listRef2.Select()
    ElseIf refid = 4 Then
        listRef4.Items.Item(refsArr(i)).Selected = True
    End If
Next

Has anyone got any ideas on this? Thanks.

有没有人对此有任何想法?谢谢。

回答by Tom F

You'll need to loop through each item in the listview list:

您需要遍历列表视图列表中的每个项目:

For I as Integer = 0 to ListView.Items.Count - 1 Do
    If ListView.Items(i).Text = "Text" then
         ListView.Items(i).Selected = true
    End If
End For

回答by matzone

You can try this ...

你可以试试这个...

For i As Integer = 0 To refsArr.Count - 1
   'find the correct category id
    Dim cmdRefCat As New SqlCommand("SELECT RefID from ReferencesListTable WHERE            RefName = '" & refsArr(i) & "'", conn)
    Dim refid As Integer = cmdRefCat.ExecuteScalar()
    Select case refid
      case 1 
        CheckIt(refsArr(i),listRef1)
      case 2 
        CheckIt(refsArr(i),listRef2)
      case 3 
        CheckIt(refsArr(i),listRef3)
      case 4
        CheckIt(refsArr(i),listRef4)
    End Select
Next

And Sub CheckIt

和子检查

Sub CheckIt(ByVal sRef as String, ByRef lvw as Listview)
    Dim x as Integer

    For x = 0 to lvw.Items.Count - 1 
        If lvw.Items(x).Text = sRef then
           lvw.Items(x).Selected = true
           exit for '-- if only 1 record   
        End If
    Next
End Sub

回答by antony thomas

The code to select an item dynamically from the listview control can be as follows for vb.net.

从列表视图控件中动态选择项目的代码对于 vb.net 可以如下所示。

  • Let lvwomominiChair1is the name of the listview control.
  • Set its fullrowselect property as true.
  • lvwomominiChair1是列表视图控件的名称。
  • 将其 fullrowselect 属性设置为 true。

The code will select the text in the first column of the listview control.

该代码将选择列表视图控件第一列中的文本。

Private Sub  lvwomominiChair1_Click(sender As Object,e As EventArgs) Handles lvwomominiChair1.Click   
    Dim lvwitem as ListViewItem
    lvwitem = lvwomominiChair1.SelectedItems.Item(0)
    MsgBox("Selected item is  " + lvwitem.Text)
End Sub

There may be situations where we need to get all items in a row of a ListView control.The following code may be used for the purpose.It is assumed that there are five columns of data in a raw and are of the text data type.This can be done with a For..Next loop as follows.Let 0,1,2,3 and 4 are the five column indices.

可能存在我们需要获取ListView控件一行中的所有项的情况。可以使用以下代码来实现。假设一个raw中有五列数据,并且是文本数据类型。这可以通过 For..Next 循环来完成,如下所示。让 0,1,2,3 和 4 是五个列索引。

Private Sub  lvwomominiChair1_Click(sender As Object,e As EventArgs) Handles lvwomominiChair1.Click   
        Dim i As Int32
        Dim str As String
        str =""
        For i =0 To 4
        str = str + " " + lvwomominiChair1.SelectedItems(0).SubItems(i).Text
        Next
        MsgBox("Selected items  of the five columns of the row are " +  str)
End Sub

回答by Daniel

Or you can do this, works perfect for me:

或者你可以这样做,非常适合我:

ListView.Items(0).Selected = True

ListView.Select()