Visual Basic- When I type the country name is the text box, it only shows the co
ID: 3803938 • Letter: V
Question
Visual Basic- When I type the country name is the text box, it only shows the country with the first letter. If I were to type a second letter, the listbox becomes empty. For example, When I type 'a', Angola is present. When I type 'an', there are no countries in the listbox. Any idea as to how I can fix that?
Public Class frmUnitedNations
Dim nations() As String = IO.File.ReadAllLines("Nations.txt")
Private Sub frmUnitedNations(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
lstOutput.DataSource = nations
lstOutput.SelectedItem = Nothing
End Sub
Private Sub txtCountry_TextChanged(sender As Object, e As EventArgs) Handles txtCountry.TextChanged
Dim query = From nation In nations
Where nation.StartsWith(txtCountry.Text.ToUpper)
Select nation
lstOutput.DataSource = query.ToList()
lstOutput.SelectedItem = Nothing
End Sub
Private Sub lstOutput_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lstOutput.Click
txtCountry.Text = lstOutput.SelectedItem
lstOutput.DataSource = nations
lstOutput.SelectedItem = Nothing
End Sub
End Class
Explanation / Answer
When a List Box control has the focus, it will automatically scroll to the first item that begins with the letter you type. It will not, however, automatically select items based on more than the first letter. For example, if you type go, you may want the control to select the first item that begins with go. This tip explains how you can implement this behaviour using the API function SendMessage. Its declaration, which must be included in the program, is as follows:
Public Declare Function SendMessage Lib "user 32"-
Alias "SendMessageA" (ByVal hwnd As Long,-
ByVal wMsg As Long, ByVal wParam As Long, -
ByVal lParam As String) As Long
ou'll also need the following constant, which tells the List Box control to select the first item that begins with the specified prefix:
Public Const LB_SELECTSTRING = &H18C
n addition to the List Box, this technique requires a Text Box control. The user enters text in the Text Box, and the List Box automatically selects the first matching item. The message is sent in the Text Box's Change event procedure:
Private Sub Text1_Change()
If Text1.Text <> "" Then
SendMessage List1.hwnd, LB_SELECTSTRING, -1, Text.Text
End If
End Sub
Letting the user select List Box items based on more than just the first letter can be particularly useful when the list contains many items
When a List Box control has the focus, it will automatically scroll to the first item that begins with the letter you type. It will not, however, automatically select items based on more than the first letter. For example, if you type go, you may want the control to select the first item that begins with go. This tip explains how you can implement this behaviour using the API function SendMessage. Its declaration, which must be included in the program, is as follows:
Public Declare Function SendMessage Lib "user 32"-
Alias "SendMessageA" (ByVal hwnd As Long,-
ByVal wMsg As Long, ByVal wParam As Long, -
ByVal lParam As String) As Long
You'll also need the following constant, which tells the List Box control to select the first item that begins with the specified prefix:
Public Const LB_SELECTSTRING = &H18C
n addition to the List Box, this technique requires a Text Box control. The user enters text in the Text Box, and the List Box automatically selects the first matching item. The message is sent in the Text Box's Change event procedure:
Private Sub Text1_Change()
If Text1.Text <> "" Then
SendMessage List1.hwnd, LB_SELECTSTRING, -1, Text.Text
End If
End Sub
Letting the user select List Box items based on more than just the first letter can be particularly useful when the list contains many items
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.