I wanted to implement a AutoComplete feature but with the values that the user has typed in this field before. It turns out that this is fairly easy to do, since AutoComplete is a property on the TextBox control in the .NET Framework. Here is a nice video that introduce the concepts surrounding AutoCompletion
So how did i do it then?
First i added a handling of the Resize-event that sets the autoComplete-properties.
Me.AutoCompleteMode = Windows.Forms.AutoCompleteMode.SuggestState.HamtaAutoCompleteLista is a function that returns the AutoCompleteStringCollection that should be used for the control.
Me.AutoCompleteSource = Windows.Forms.AutoCompleteSource.CustomSource
Me.AutoCompleteCustomSource = State.HamtaAutoCompleteLista(Me.Name)
As you might notice I am using the name as key, to which you might have objections. Of course it would have been better to be using a more meaningful name that can be shared among several controls. If I want the same auto completion list to be shown for two textboxes they need to have the same name. For my purposes this is ok.
Then i did an override on the OnLostFocus-event to add new items into the AutoCompleteCollection.
Protected Overrides Sub OnLostFocus(ByVal e As System.EventArgs)
' Don't add blank
If Not String.IsNullOrEmpty(Me.Text) Then
Dim autoCompleteLista As AutoCompleteStringCollection = State.HamtaAutoCompleteLista(Me.Name)
' Check if the text exists already - then don't add it
If Not autoCompleteLista.Contains(Me.Text) Then
So as you can see it wasn't that much code to accomplish a dynamical AutoComplete-function in a textbox.
I had some trouble to save it to User Setttings... but that will be described in another post, maybe.
Published byon Last updated