vb.net select listviewitem by giving it's index

209 views Asked by At

I'm searching everywhere but I cannot seem to find the answer.

In VB.net I want to select a listviewitem by giving it's index.

Let say: If I push a button it has to select row 5.

I don't seem to get it to work...

Can anyone help me?

Thanks

1

There are 1 answers

0
Calaf On

Let's assume the following situation:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    ListView1.Items.Add("Item 1") 'Index 0
    ListView1.Items.Add("Item 2") 'Index 1
    ListView1.Items.Add("Item 3") 'Index 2
End Sub

You can select an item by using:

    ListView1.Items.Item(index).Selected = True

For example:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    ListView1.Items.Item(1).Selected = True

    'Not necessary, just to show the blue color instead of gray
    ListView1.Select()
End Sub

Here is the result before and after pressing Button1.

Before After