在索引中选择VB.NET列表框



在我的代码中,我选择excel文件将其添加到列表框中,然后运行一些代码,替换列表中所有excel文件上的某个单元格。每次它更改excel文件中的单元格时,都会使用转到列表框中的下一个单元格

Me.ListBox2.SelectedIndex = Me.ListBox2.SelectedIndex + 1

但当它到达列表的末尾时,它会给我一个错误。我怎么能让我的列表框知道它已经结束了。

像这样进行检查:

if Me.ListBox2.SelectedIndex + 1  < Me.ListBox2.items.count then
    Me.ListBox2.SelectedIndex + = 1
End if

我想我会做一个For/Each/Next来循环浏览这样的项目:

For Each ListItem As Object In ListBox2.Items
    'Set Active item as selected
    ListBox2.SelectedItem = ListItem
    'manipulate file
    UpdateExcelFile(ListItem.ToString)
Next

只是为了好玩。您可以在不使用IF/END IF的情况下执行此操作
以下代码将把所选的索引移到下一个项目,直到它到达列表框的末尾。

ListBox1.SelectedIndex += Math.Abs(CInt(ListBox1.SelectedIndex < ListBox1.Items.Count - 1))

最新更新