You're reading...

AutoComplete Textbox


During the work on a project, i need to use a control having auto complete facility in it as user types then I realized that we can make use of the textbox control  or combobox control for this purpose. Both controls can be used to filter records as a drop down list to show the best matches.

I will demonstrate it using VB.Net. If someone needs a C# version, an online converter can be use. I will be discussing the addition and removal of item from the drop down list.


Before reading this article we need to be aware of two properties and an enumeration introduces by Microsoft in .Net framework 2.0 which is AutoCompleteCustomSource Property, AutoCompleteMode property and AutoCompleteSource Enumeration.

We should also have a look at AutoCompleteStringCollection Class.


An auto complete source is binded to the textbox’s AutoCompleteCustomSource property. It will help to filter the best fit records in the suggestion list.

‘Item is filled either manually or from database

Dim lst As New List(Of String)

‘AutoComplete collection that will help to filter keep the records.

Dim MySource As New AutoCompleteStringCollection()


I have filled the source from the list named ‘lst’ at Form1_Load event. This list can be populated by database as well.

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

'Manually added some items
lst.Add("apple") : lst.Add("applle") : lst.Add("appple") lst.Add("appplee") : lst.Add("bear") : lst.Add("pear")

'Records binded to the AutocompleteStringCollection.

'this AutocompleteStringcollection binded to the textbox as custom source.
TextBox1.AutoCompleteCustomSource = MySource

'Auto complete mode set to suggest append so that it will sugesst one 'or more suggested completion strings it has both ‘Suggest’ and '‘Append’ functionality
TextBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend

'Set to Custom source we have filled already
TextBox1.AutoCompleteSource = AutoCompleteSource.CustomSource

End Sub

Operations on AutoCompleteSource:

As I have discussed earlier that we will see how to add/ remove entry from the source we have binded to the Textbox’s source.

The event uses for this task to achieve is KeyDown event.

Here is the source with explanation:

Private Sub TextBox1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
' On enter I planned to add it the list
If e.KeyCode = Keys.Enter Then   

' If item not present already
If Not lst.Contains(TextBox1.Text) Then

' Add to the source directly
TextBox1.AutoCompleteCustomSource.Add(TextBox1.Text) End If

'On delete key, planned to remove entry
ElseIf e.KeyCode = Keys.Delete Then

' declare a dummy source
Dim coll As AutoCompleteStringCollection = TextBox1.AutoCompleteCustomSource

' remove item from new source

' Bind the updates
TextBox1.AutoCompleteCustomSource = coll

' Clear textbox

End If End Sub


There are more details as to how the whole thing works. I feel this is a viable solution for an Auto-Complete TextBox and I hope you find it interesting.

About Shahan Ayyub

Shahan is a Microsoft Certified Professional of .Net. Mostly self taught programmer and a quick learner, who just love to code and always eager to find unique solutions to the common problems. Currently working as an iOS developer.


No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: