Lloyd Sheen said:
Ok try this.
Instead of a textbox use a combobox. Set
Me.ComboBox1.AutoCompleteMode =
System.Windows.Forms.AutoCompleteMode.Suggest
Me.ComboBox1.AutoCompleteSource =
System.Windows.Forms.AutoCompleteSource.ListItems
On the form:
Me.KeyPreview = True
Now the code:
Dim myList As New List(Of String)
Private Sub Form1_KeyPress(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
Dim t As String = String.Empty
If Not Char.IsLetter(e.KeyChar) Then
Exit Sub
End If
If Me.ComboBox1.Focused Then
Dim tmp As String
Dim tmp2 As String
Dim ss As Integer = ComboBox1.SelectionStart
tmp = ComboBox1.Text.Substring(0, ComboBox1.SelectionStart) +
e.KeyChar.ToString + ComboBox1.Text.Substring(ComboBox1.SelectionStart)
tmp2 = tmp.ToUpper
ComboBox1.Items.Clear()
For Each s As String In myList
If s.ToUpper.Contains(tmp2) Then
ComboBox1.Items.Add(s)
End If
Next
ComboBox1.Text = tmp
ComboBox1.SelectionStart = ss + 1
If ComboBox1.Items.Count > 0 Then
ComboBox1.DroppedDown = True
End If
End If
e.Handled = True
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
myList.Add("Big Mac")
myList.Add("Little Mac")
myList.Add("Quarter Pounder")
myList.Add("Cheeseburger")
End Sub
The loading of the items I can leave to you and you will notice that I
have a filter
If Not Char.IsLetter(e.KeyChar) Then
Exit Sub
End If
in sub. You may want to change this to handle other characters than
letters but you will want to filter out handling the control characters.
Hope this helps
Lloyd Sheen
Sorry for last one I have a much simpler one:
Just one combobox. No items. No autosuggest settings.
Then the following code:
Public Class Form1
Dim myList As New List(Of String)
Dim shownList As New List(Of String)
Dim ss As Integer
Private Sub Form1_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
myList.Add("Big Mac")
myList.Add("Little Mac")
myList.Add("Quarter Pounder")
myList.Add("Cheeseburger")
End Sub
Private Sub ComboBox1_DropDown(ByVal sender As Object, ByVal e As
System.EventArgs) Handles ComboBox1.DropDown
SetNewItems()
End Sub
Private Sub SetNewItems()
ComboBox1.Items.Clear()
For Each s As String In shownList
ComboBox1.Items.Add(s)
Next
ComboBox1.SelectionStart = ss
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Timer1.Tick
Timer1.Enabled = False
If shownList.Count > 0 Then
ss = ComboBox1.SelectionStart
If ComboBox1.DroppedDown = True Then
SetNewItems()
Else
ComboBox1.DroppedDown = True
End If
End If
End Sub
Private Sub ComboBox1_TextChanged(ByVal sender As Object, ByVal e As
System.EventArgs) Handles ComboBox1.TextChanged
Dim tmp2 As String = ComboBox1.Text.ToUpper
'ComboBox1.DroppedDown = False
shownList.Clear()
If tmp2 <> String.Empty Then
For Each s As String In myList
If s.ToUpper.Contains(tmp2) Then
shownList.Add(s)
End If
Next
End If
If shownList.Count > 0 Then
Timer1.Interval = 100
Timer1.Enabled = True
Else
ComboBox1.DroppedDown = False
End If
End Sub
End Class
Think this is it.
Lloyd Sheen