Click here to Skip to main content
11,925,246 members (55,684 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


20 bookmarked

AutoComplete Textbox

, 30 Dec 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
This article helps to create a suggestion list with autocomplete facility, add/remove operation is also discussed.


During work on a project, I needed to use a control having auto complete facility in it as user types. 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 used. 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 introduced by Microsoft in the .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 the database as well.

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

    'Manually added some items

    'Records binded to the AutocompleteStringCollection.

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

    'Auto complete mode set to suggest append so that it will sugesst one
    'or more suggested completion strings it has bith ‘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 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

If e.KeyCode = Keys.Enter Then   ' On enter I planned to add it the list
     If Not lst.Contains(TextBox1.Text) Then  ' If item not present already
        ' Add to the source directly
     End If
ElseIf e.KeyCode = Keys.Delete Then 'On delete key, planned to remove entry

' 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 of ‘KeyCode’ condition

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.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Shahan Ayyub
Software Developer eHealthWorks LLC
Pakistan Pakistan
I satisfy my daily 'urge to code' by participating in forums:

On Experts-Exchange:
Screen Name : Shahan_Developer

Screen Name : EngrShahan

On C-Sharp Corner:
Screen Name : ShahanDev

On DaniWeb IT Community:
Screen Name : ShahanDev

You may also be interested in...

Comments and Discussions

GeneralVery useful Pin
alecs.mkd12-Jun-15 3:29
memberalecs.mkd12-Jun-15 3:29 
AnswerRe: Error AutoComplete "AccessViolationException" Pin
om3n30-Jul-12 19:55
memberom3n30-Jul-12 19:55 
GeneralRe: Error AutoComplete "AccessViolationException" Pin
Shahan Ayyub30-Jul-12 19:58
memberShahan Ayyub30-Jul-12 19:58 
QuestionError AutoComplete "AccessViolationException" Pin
Member 928154029-Jul-12 18:28
memberMember 928154029-Jul-12 18:28 
AnswerRe: Error AutoComplete "AccessViolationException" Pin
Shahan Ayyub30-Jul-12 8:55
memberShahan Ayyub30-Jul-12 8:55 
QuestionRe: Error AutoComplete "AccessViolationException" Pin
om3n30-Jul-12 18:51
memberom3n30-Jul-12 18:51 
QuestionMore like a Tip Pin
PIEBALDconsult30-Dec-11 5:36
memberPIEBALDconsult30-Dec-11 5:36 
AnswerRe: More like a Tip Pin
Shahan Ayyub3-Jan-12 0:05
memberShahan Ayyub3-Jan-12 0:05 
GeneralMy vote of 5 Pin
Ahmed_online30-Dec-11 3:33
memberAhmed_online30-Dec-11 3:33 
GeneralMy vote of 2 Pin
khaled.mufti27-Dec-11 20:19
memberkhaled.mufti27-Dec-11 20:19 
GeneralMy vote of 5 Pin
Mavusana23-Dec-11 2:20
memberMavusana23-Dec-11 2:20 
Shahan you're the man. Thanks for sharing, highly appreciated.
Generalthank you a lot Pin
Amila thennakoon10-Sep-11 7:33
memberAmila thennakoon10-Sep-11 7:33 
GeneralRe: thank you a lot Pin
Shahan Ayyub10-Sep-11 7:57
memberShahan Ayyub10-Sep-11 7:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.151125.3 | Last Updated 30 Dec 2011
Article Copyright 2011 by Shahan Ayyub
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid