从列表框中选择Line,然后转换为字符串

时间:2011-11-19 02:31:30

标签: vb.net string listbox

我想在列表框中搜索一小段文本,如果它在列表框中我想选择它,然后将其转换为字符串。

我该怎么做?

因为我找不到在特定行上选择某些内容的好命令!

由于

1 个答案:

答案 0 :(得分:1)

要选择ListBox项,请设置ListBox的SelectedIndex属性。所以,例如:

Dim stringToFind As String = "someString"

For i As Integer = 0 To Me.MyListBox.Items.Count - 1
    Dim itemAsString As String = Me.MyListBox.Items(i).ToString()
    If itemAsString.Contains(stringToFind) Then
        Me.MyLabel.Text = itemAsString
        Me.MyListBox.SelectedIndex = i
        Exit For 'If you're using a MultiSelect ListBox, you can add to Me.MyListBox.SelectedIndices and remove this line.
    End If
Next