user1054822
user1054822

Reputation: 21

Select Line from listbox and then convert to string

I want to search for a small bit of text in my listbox, if it's in the listbox I want to select it, and then covert it to a string.

How do I do this?

Because I cannot find the good command to select something on a specific line!

Thanks

Upvotes: 0

Views: 3519

Answers (1)

Ry-
Ry-

Reputation: 224903

To select a ListBox item, set the ListBox's SelectedIndex property. So, for example:

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

Upvotes: 1

Related Questions