VB.NET 导航到特定的列表框项目

VB.NET navigate to a specific ListBox item

我有一个用于加载项目的列表框。每行以 HH:mm:ss 格式的时间开头。我想要一个按钮,以便在单击它时,列表框选择的项目将导航到以特定时间开头的行,如用户输入到文本框的那样。其次,我有一个文本框,使用 selecteditem.text.tostring.substring(0,5) 复制所选项目的前 5 个字符。现在,我需要捕获所选项目 RIGHT BELOW 行的前 5 个字符。感谢您的帮助。

ListBox1.SelectedIndex = 2

0等于第一行
1 个相等的第二行
2等于第三行
然后就这样结束

或者你可以像下面这样在最后加上-1

ListBox1.SelectedIndex = 2 - 1

所以你可以select实际行号二

使用 ListBox 的 FindString() 方法,您可以找到以指定字符串开头的第一项的索引。然后,您可以使用它来设置 SelectedIndex 属性,这将 select 指定索引处的项目。

要获得低于当前 selected 的项目,您只需从 Items collection.

中获得 SelectedIndex + 1
Public Sub DoSomething()
    Dim Index As Integer = ListBox1.FindString(TextBox1.Text) 'Find the index of the item starting with whatever is in TextBox1.
    If Index > -1 Then 'Check if the item exists/was found.
        ListBox1.SelectedIndex = Index
        TextBox2.Text = ListBox1.Items(Index).ToString().Substring(0, 5)

        If Index < ListBox1.Items.Count - 1 Then 'Check if the found item is the last item or not.
            TextBox3.Text = ListBox1.Items(Index + 1).ToString().SubString(0, 5)
        Else 'This was the last item.
            MessageBox.Show("You've reached the end of the list.", "", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If

    Else 'No item was found.
        MessageBox.Show("No item found starting with the specified text!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End If
End Sub