Check if the listbox index exists?

Asked

Viewed 180 times

0

Hello wanted to know how to identify if the index (Example: 5 does not exist) and return to 0.

Follows my code:

Dim avancarcoordX As Integer
avancarcoordX = lbPosX.SelectedIndex + 1
lbPosX.SelectedIndex = avancarcoordX
PosX.Text = lbPosX.SelectedItem
Dim avancarcoordY As Integer
avancarcoordY = lbPosY.SelectedIndex + 1
lbPosY.SelectedIndex = avancarcoordY
PosY.Text = lbPosY.SelectedItem 

When there is no Item then it presents an error. I need that on reaching the last item (Which is variable) it returns to the first one. So an infinite loop.

1 answer

0

Good night.

Try the following code:

    If ListBox1.Items.Count > 0 Then

        If ListBox1.SelectedIndex = -1 Then
            ListBox1.SelectedIndex = 0
        Else
            If ListBox1.SelectedIndex < ListBox1.Items.Count - 1 Then
                ListBox1.SelectedIndex += 1
            Else
                ListBox1.SelectedIndex = 0
            End If
        End If
    End If

Browser other questions tagged

You are not signed in. Login or sign up in order to post.