|
|
Title | Let the user type a prefix string to select an item in a ListBox in VB .NET |
Keywords | ListBox, select item |
Categories | Controls, VB.NET |
|
|
My book Ready-to-Run Visual Basic Code Library shows how to let the user type in a TextBox and have the program select the first ListBox item that matches the text. That example uses an API function to tell the ListBox to select the matching item.
This example does something similar in VB .NET. The features of many use API functions have been incorporated into VB .NET controls and this is the case here. The VB .NET ListBox control has a FindString method that returns the index of the first item matching some text. The program uses this value to set the control's SelectedIndex property.
|
|
Private Sub txtSelection_TextChanged(ByVal sender As _
System.Object, ByVal e As System.EventArgs) Handles _
txtSelection.TextChanged
lstWords.SelectedIndex = _
lstWords.FindString(txtSelection.Text)
End Sub
|
|
|
|
|
|