StealthRT StealthRT - 2 months ago 6
Vb.net Question

Moving listview up and down by buttons

I am trying to use 2 buttons (up and down) to move up and down in the listview highlighting each item depending on what direction its going in the listview.

This is my current code:

Private Sub cmdDown_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDown.Click
If cmdDown.Tag <> totalRecordsReturned Then
cmdDown.Tag += 1
ListView1.Items(cmdDown.Tag).Selected = True
ListView1.Focus()
End If
End Sub


That does work But it selects more items every time i hit the button. What can i do in order to correct this?

David

Answer

In Design View, select Multiselect = False

or add this one line in your code

ListView1.MultiSelect = False

like this

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    ListView1.MultiSelect = False
End Sub

Private Sub cmdDown_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles cmdDown.Click
    If cmdDown.Tag <> totalRecordsReturned Then
        cmdDown.Tag += 1
        ListView1.Items(cmdDown.Tag).Selected = True
        ListView1.Focus()
    End If
End Sub
Comments