SucksAtAssembly
SucksAtAssembly

Reputation: 71

SelectedIndex is set to 1, after being set to 0

I've created a code where it checks if the last item is selected and then goes to the first one, giving off a loop feel.

Private Sub Main_KeyDown(sender As System.Object, e As KeyEventArgs) Handles FilesBox.KeyDown
    If FilesBox.SelectedIndex = FilesBox.Items.Count - 1 Then
        Last += 1
        If Last = 1 Then
            Last = 0
            FilesBox.SelectedIndex = 0
        End If
    End If
End Sub

When the last item is selected the value "Last" will increment by 1, making sure when you have pressed the right key the first time you have it selected and after you press again, it will go to the first item.

It works fine as intended but somehow after the index is set to 0, it changes to 1 from nowhere which goes to the second item on the list... Any ideas what is going on?

Upvotes: 1

Views: 49

Answers (1)

LarsTech
LarsTech

Reputation: 81610

You have stop the control from processing that key event. After you set the index to zero, the keyboard action is still moving the item down a row:

Private Sub FilesBox_KeyDown(sender As Object, e As KeyEventArgs) _
                                               Handles FilesBox.KeyDown
  If FilesBox.SelectedIndex = FilesBox.Items.Count - 1 Then
    FilesBox.SelectedIndex = 0
    e.SuppressKeyPress = True
  End If
End Sub

Upvotes: 2

Related Questions