混合 .selectindex 和 .selectitem 如何检测循环的负值

mixing .selectindex and .selectitem how to detect negative values for loop

我有 1 个 PictureBox、1 个 ListBox 到 select 一个文件(文件名)、1 个特定路径(lastfoldr),它正确加载图像并更改为下一个和上一个图像,但我试图制作一个当用户在最后一张照片时尝试切换到下一张图像时的图像循环。

Private Sub ShowPrevImage()
    ListBox5.SelectedIndex = -1
    'If ListBox5.SelectedIndex < 1 Then 'here is the problem
    '    ListBox5.SelectedIndex = ListBox5.Items.Count - 1
    'End If
    Me.PictureBox1.Image = Image.FromFile(lastfoldr & (ListBox5.SelectedItem))
End Sub

Private Sub ShowNextImage()
    ListBox5.SelectedIndex = +1
  '  If ListBox5.SelectedIndex > ListBox5.Items.Count Then 'here is the problem too
   '     ListBox5.SelectedIndex = 0
   ' End If
    Me.PictureBox1.Image = Image.FromFile(lastfoldr & (ListBox5.SelectedItem)) 
end if

您的 ListBox 索引从 0 变为 ListBox.Items.Count -1,因此每当您使用 ShowNextImage 检查索引是否等于 Count-1 时。如果是,将索引设置回 0

Private Sub ShowNextImage()
    If LisBox5.SelectedIndex = ListBox5.Items.Count-1 then
        ListBox5.SelectedIndex = 0
    Else
        ListBox5.SelectedIndex +=1
    End If