Click here to Skip to main content
15,885,366 members
Articles / Desktop Programming / WPF
Article

WPF Autocomplete Textbox Control

Rate me:
Please Sign up or sign in to vote.
4.52/5 (35 votes)
31 May 2008CPOL3 min read 238K   17.1K   91   43
An article showing how to build a WPF autocomplete textbox control with delay input

Introduction

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.

Background

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 AskErnest.com 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.

XML
xmlns:local="clr-namespace:WPFAutoCompleteTextbox"

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

XML
<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.

C#
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.

History

  • 30th May, 2008: First release

License

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


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralNice One Pin
kapil bhavsar7-May-09 23:49
kapil bhavsar7-May-09 23:49 
GeneralMy vote of 2 Pin
RandomEngy14-Apr-09 7:44
RandomEngy14-Apr-09 7:44 
QuestionWait hold on... is your ComboBox hiding behind the text field? Pin
RandomEngy14-Apr-09 7:41
RandomEngy14-Apr-09 7:41 
GeneralThanks!! Pin
DZaK8319-Mar-09 13:11
DZaK8319-Mar-09 13:11 
GeneralAnother alternative Pin
toidy30-Sep-08 7:15
toidy30-Sep-08 7:15 
Generalkeyboard oriented selection Pin
john c gibbons22-Sep-08 5:24
john c gibbons22-Sep-08 5:24 
QuestionNo keyboard navigation? Pin
armentage17-Jul-08 4:59
armentage17-Jul-08 4:59 
GeneralComboBox Selection Pin
Andrewiski2-Jun-08 15:54
Andrewiski2-Jun-08 15:54 
I love the example it was perfect for what I am trying to accomplish. I have a question and wondering if you have ran into it and had a solution.

When you type and there is a match on one or more items the combobox appears but a down arrow will not highlight the first item. If you move the mouise over the first item it highlights and then the arrows will work and once you have highlighted the item you want enter makes the selection.

Any Idea on how to get the comboBox to highlight (not Select) the first item so arrow down will traverse the list. I have tried a bunch of differnt approches but can't seem to find a way to accomplish this with out selecting the first value.

Thanks for the help.

Andy

Andrew DeVries has been a .Net programmer since 2003. He currently works as a consultant designing custom software for windows as well as for the web. Currently he spends his days designing custom web controls and custom data sources.

GeneralRe: ComboBox Selection Pin
thomastn2-Jun-08 18:18
thomastn2-Jun-08 18:18 
GeneralRe: ComboBox Selection Pin
Andrewiski5-Jun-08 10:24
Andrewiski5-Jun-08 10:24 
QuestionChanging data set? Pin
Jim Weiler31-May-08 10:32
Jim Weiler31-May-08 10:32 
AnswerRe: Changing data set? [modified] Pin
thomastn2-Jun-08 17:53
thomastn2-Jun-08 17:53 
GeneralRe: Changing data set? Pin
boriska00215-Sep-09 1:39
boriska00215-Sep-09 1: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.