Creating a dynamical AutoComplete textbox

Posted by Marcus Hammarberg on November 14, 2007
Stats
In my recent project we have had to implement a own textbox to be able to set a few properties that force the user to follow certain formats etc. A few days ago i saw this as an oppurtunity to do something cool as an additional feature.

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.Suggest
Me.AutoCompleteSource = Windows.Forms.AutoCompleteSource.CustomSource
Me.AutoCompleteCustomSource = State.HamtaAutoCompleteLista(Me.Name)
State.HamtaAutoCompleteLista is a function that returns the AutoCompleteStringCollection that should be used for the control.

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
autoCompleteLista.Add(Me.Text)
End If
End If
MyBase.OnLostFocus(e)
End Sub


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 by Marcus Hammarberg on Last updated