【问题标题】:VB.NET: How to dynamically select a list view item?VB.NET:如何动态选择列表视图项?
【发布时间】:2013-05-15 19:17:42
【问题描述】:

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

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

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

到目前为止,我有这个:

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

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

【问题讨论】:

    标签: vb.net listview dynamic selection


    【解决方案1】:

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

    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
    

    【讨论】:

      【解决方案2】:

      你可以试试这个……

      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
      

      和子检查

      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
      

      【讨论】:

      • 遍历列表视图和数组并比较每个都可以,但这是一个不错的解决方案。谢谢!
      【解决方案3】:

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

      • lvwomominiChair1 是列表视图控件的名称。
      • 将其 fullrowselect 属性设置为 true。

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

      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
      

      可能存在我们需要获取一个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
      

      【讨论】:

        【解决方案4】:

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

        ListView.Items(0).Selected = True
        
        ListView.Select()
        

        【讨论】:

          猜你喜欢
          • 2014-07-31
          • 2015-03-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-12-03
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多