Click here to Skip to main content
13,249,813 members (67,817 online)
Click here to Skip to main content
Add your own
alternative version


91 bookmarked
Posted 31 May 2008

WPF Autocomplete Textbox Control

, 31 May 2008
Rate this:
Please Sign up or sign in to vote.
An article showing how to build a WPF autocomplete textbox control with delay input


This article demonstrates some basic steps on how to build an auto complete textbox custom control with WPF. I have looked at many auto completion textbox solutions, most of them suffered performance issues when filled with large amount of entries. This implementation attempts to mask that problem by not building the suggested list until the user is done typing.


I needed an autocompletion textbox for my application that can search based not just on name but also on keywords. I found the solution posted by pfemiani. I like his idea of searching by keywords but the solution does not work for my WPF application without extensive modification. Then I came across a WPF solution from using a textbox and combobox controls, so I decided to build my code upon that.

Ernest's suggestion is pretty simple, there are two children in the control, Textbox and Combobox. On the TextBox's TextChanged event, we would populate the list for the combobox using words matching the text in the textbox. On the ComboBox's SelectionChanged event, we set the text in the textbox using the content of the selected item of the Combobox. This works very well except that Ernest is only doing name matching and his blog doesn't provide the complete source code.

Another challenge that I'm facing is performance issue. My autocompletion textbox is backed by several thousand entries from a database, on some keyword I could have over 300 hits. Populating the combobox on every user keystroke feels sluggish. My solution is to start a timer on TextChanged event and populate the combobox list only when the timer expires. I tried this on my large database and it works beautifully. I can type a long name and not feel like I'm on a 286.

Using the Code

To use this custom control in your application, you simply add the following files...

  1. AutoCompleteEntry.cs
  2. AutoCompleteTextBox.xaml
  3. AutoCompleteTextBox.xaml.cs

... to your project. Next, in the XAML file that you would like to use the auto complete textbox custom control, add this line to the header block.


Then in the XAML code, where you want to place the auto complete textbox, add:

<local:AutoCompleteTextBox Height="23" Width="240" x:Name="textBox1" 

    DelayTime="500" Threshold="2"/>

In my example code, the name of the textbox is textBox1. You can get or set the text in the textbox by referring to textBox1.Text. The DelayTime property is the amount of time (in ms) that you want to delay after the user starts typing before populating the list. Leaving the DelayTime unset will default to 0, which is populating immediately after each user keystroke. The Threshold property controls the number of characters threshold, at or over that at we will start suggesting.

In the code behind of my example, I manually populate the search entries with a variety of cars and models.

textBox1.AddItem(new AutoCompleteEntry
    ("Toyota Camry", "Toyota Camry", "camry", "car", "sedan"));
textBox1.AddItem(new AutoCompleteEntry
    ("Toyota Corolla", "Toyota Corolla", "corolla", "car", "compact"));
textBox1.AddItem(new AutoCompleteEntry
    ("Toyota Tundra", "Toyota Tundra", "tundra", "truck"));
// null matching string will default with just the name
textBox1.AddItem(new AutoCompleteEntry("Chevy Impala", null));
textBox1.AddItem(new AutoCompleteEntry
    ("Chevy Tahoe", "Chevy Tahoe", "tahoe", "truck", "SUV"));
textBox1.AddItem(new AutoCompleteEntry
    ("Chevrolet Malibu", "Chevrolet Malibu", "malibu", "car", "sedan"));

If for example, the user types in "car", all the entries with the "car" keyword will be suggested.

I hope you find this solution helpful. If you find any errors or have questions, feel free to contact me.


  • 30th May, 2008: First release


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


About the Author

United States United States
No Biography provided

You may also be interested in...


Comments and Discussions

QuestionAutocomplete textbox in swing Pin
Member 1352221915-Nov-17 20:35
memberMember 1352221915-Nov-17 20:35 
QuestionCan you use Focus to acess this control? Pin
AIshihara16-Jul-15 9:46
memberAIshihara16-Jul-15 9:46 
Questionproblem with keyboard selection Pin
Kanhaiya Chaurasiya12-Apr-15 8:23
memberKanhaiya Chaurasiya12-Apr-15 8:23 
GeneralMy vote of 5 Pin
DoThanhTuan27-Oct-14 18:06
memberDoThanhTuan27-Oct-14 18:06 
GeneralRe: My vote of 5 Pin
Member 111576155-Mar-15 5:18
memberMember 111576155-Mar-15 5:18 
GeneralThanks! Pin
Member 98882526-Jul-14 5:56
memberMember 98882526-Jul-14 5:56 
QuestionVery useful job Pin
andbura7-Feb-14 23:52
memberandbura7-Feb-14 23:52 
QuestionHow to connect with database values. Pin
kamalihere25-Oct-13 22:41
memberkamalihere25-Oct-13 22:41 
GeneralThanks Pin
wangsy8011-Jun-12 18:57
memberwangsy8011-Jun-12 18:57 
QuestionAdd ItemTemplate Pin
Jenami676-Apr-12 22:13
memberJenami676-Apr-12 22:13 
GeneralMy vote of 5 Pin
shynet25-Dec-11 10:27
membershynet25-Dec-11 10:27 
GeneralThanks. An alternative way. Pin
poporopo15-Jun-11 10:29
memberpoporopo15-Jun-11 10:29 
GeneralMy vote of 4 Pin
lixie132422-May-11 18:02
memberlixie132422-May-11 18:02 
GeneralAccept items only from list Pin
boki_thegame26-Mar-11 8:46
memberboki_thegame26-Mar-11 8:46 
GeneralMy vote of 5 Pin
airtongomeslima17-Mar-11 17:46
memberairtongomeslima17-Mar-11 17:46 
QuestionHow can i reference a class from the AutoCompleteBox Pin
abetterword7-Jan-11 10:13
memberabetterword7-Jan-11 10:13 
GeneralMy vote of 5 Pin
vegbruiser27-Oct-10 4:03
membervegbruiser27-Oct-10 4:03 
Generalone more thing Pin
dotNiemand27-May-10 1:31
memberdotNiemand27-May-10 1:31 
GeneralSelected Item Event improvement Pin
dotNiemand27-May-10 1:06
memberdotNiemand27-May-10 1:06 
Generalsmall improvement - down button Pin
dotNiemand27-May-10 0:49
memberdotNiemand27-May-10 0:49 
GeneralAdding focus after selecting from the combobox. Pin
Budsy29-Mar-10 23:27
memberBudsy29-Mar-10 23:27 
Questionadditional Pin
Coman Ovidiu24-Feb-10 2:39
memberComan Ovidiu24-Feb-10 2:39 
AnswerRe: additional Pin
Budsy29-Mar-10 23:36
memberBudsy29-Mar-10 23:36 
Question[My vote of 1] Hey - what's this keyboard for? Pin
tonyt21-Feb-10 17:24
membertonyt21-Feb-10 17:24 
GeneralIssue with WPF AutoComplete TextBox with Chinese and Japanese inputs Pin
sandyyyyyyyyyy19-Nov-09 0:35
membersandyyyyyyyyyy19-Nov-09 0:35 
GeneralRe: Issue with WPF AutoComplete TextBox with Chinese and Japanese inputs Pin
Max.Liu.Bela9-Oct-14 17:17
memberMax.Liu.Bela9-Oct-14 17:17 
GeneralMy vote of 2 Pin
realvasche23-Oct-09 8:36
memberrealvasche23-Oct-09 8:36 
Questionchinese IME issue Pin
nnnnimisha5-Aug-09 21:17
membernnnnimisha5-Aug-09 21:17 
QuestionHow can I change the Background Color of the AutoComplete TextBox? Pin
microsoft_kc23-Jun-09 21:59
membermicrosoft_kc23-Jun-09 21:59 
AnswerRe: How can I change the Background Color of the AutoComplete TextBox? Pin
Trond Andersen22-Oct-09 5:08
memberTrond Andersen22-Oct-09 5:08 
GeneralNice One Pin
kapil bhavsar8-May-09 0:49
memberkapil bhavsar8-May-09 0:49 
GeneralMy vote of 2 Pin
RandomEngy14-Apr-09 8:44
memberRandomEngy14-Apr-09 8:44 
QuestionWait hold on... is your ComboBox hiding behind the text field? Pin
Member 403000514-Apr-09 8:41
memberMember 403000514-Apr-09 8:41 
GeneralThanks!! Pin
DZaK8319-Mar-09 14:11
memberDZaK8319-Mar-09 14:11 
GeneralAnother alternative Pin
toidy30-Sep-08 8:15
membertoidy30-Sep-08 8:15 
Generalkeyboard oriented selection Pin
john c gibbons22-Sep-08 6:24
memberjohn c gibbons22-Sep-08 6:24 
kinda hacky but quick, adding this handler registration

textBox.PreviewKeyDown += new KeyEventHandler(textBox_PreviewKeyDown);

with a handler like this guy

void textBox_PreviewKeyDown(object sender, KeyEventArgs e)<br />
{<br />
    if (comboBox.Items.Count > 0 && e.Key == Key.Down)<br />
    {<br />
        comboBox.Focus();<br />
    }<br />
}<br />

allow for a simple move to the combobox for keyboard oriented selection like you might see after the matching results pop as in google suggested search, others.

good code!
QuestionNo keyboard navigation? Pin
armentage17-Jul-08 5:59
memberarmentage17-Jul-08 5:59 
GeneralComboBox Selection Pin
Andrewiski2-Jun-08 16:54
memberAndrewiski2-Jun-08 16:54 
GeneralRe: ComboBox Selection Pin
thomastn2-Jun-08 19:18
memberthomastn2-Jun-08 19:18 
GeneralRe: ComboBox Selection Pin
Andrewiski5-Jun-08 11:24
memberAndrewiski5-Jun-08 11:24 
QuestionChanging data set? Pin
Jim Weiler31-May-08 11:32
memberJim Weiler31-May-08 11:32 
AnswerRe: Changing data set? [modified] Pin
thomastn2-Jun-08 18:53
memberthomastn2-Jun-08 18:53 
GeneralRe: Changing data set? Pin
boriska00215-Sep-09 2:39
memberboriska00215-Sep-09 2:39 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171114.1 | Last Updated 31 May 2008
Article Copyright 2008 by thomastn
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid