Check if the index of the listbox exists?

0

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

Follow 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 has an error. I need to get back to the last item (which is variable). So an infinite loop.

    
asked by anonymous 25.04.2017 / 04:12

1 answer

0

Good evening.

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
    
25.04.2017 / 04:31