
12-05-2005, 12:34 AM
|
|
Senior Contributor
Forum Leader * Expert *
|
|
Join Date: Feb 2005
Location: London
Posts: 1,050
|
|
You need the listviewitem itself to use listview.items.indexof, you can't just search by name. To get the listviewitem you just loop through all listview items in the listview with a for each ... in ... next loop:
Code:
' there's a form with listview1 and button1 on it...
Dim randomGenerator As New Random
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ListView1.View = View.Details
ListView1.Columns.Add("Name", 100, HorizontalAlignment.Left)
ListView1.MultiSelect = False
For i As Integer = 0 To 15
ListView1.Items.Add("Item " & i)
Next
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim i As Integer = randomGenerator.Next(0, 16) ' search for a random item
'loop through each item in the listview
For Each lvi As ListViewItem In ListView1.Items()
' test to see if it is our item
If lvi.Text = "Item " & i Then
' if it is then select it
lvi.Selected = True
Else
' if listview1.multiselect were = false then you could deselect any other selections here
End If
Next
' the button is selected so you wont see anything:
ListView1.Select()
End Sub
|
|