title: VB.NET: How to dynamically select a list view item?
body: <p>I need to dynamically select an item in a listview based on what was selected previously.</p>
  
  <p>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.</p>
  
  <p>Doing this by index like so <code>listRef1.Items(2).Checked = True</code> is no problem but I need to do it by the item text, i.e. one of the strings in the array. </p>
  
  <p>So far I have this:</p>
  
  <pre><code>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 = '" &amp; refsArr(i) &amp; "'", 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
  </code></pre>
  
  <p>Has anyone got any ideas on this? Thanks.</p>