Click here to Skip to main content
6,917,664 members and growing! (13,587 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » Windows Presentation Foundation » Controls     Beginner License: The Code Project Open License (CPOL)

WPF Autocomplete Textbox Control

By thomastn

An article showing how to build a WPF autocomplete textbox control with delay input
C#3.0, Windows, .NET3.0, .NET3.5, WPF, Dev
Posted:31 May 2008
Views:38,772
Bookmarked:44 times
printPrint Friendly   add Share
      Discuss Discuss   Broken Article?Report  
17 votes for this article.
Popularity: 4.44 Rating: 3.61 out of 5
1 vote, 5.9%
1
4 votes, 23.5%
2
2 votes, 11.8%
3
2 votes, 11.8%
4
8 votes, 47.1%
5

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.

xmlns:local="clr-namespace:WPFAutoCompleteTextbox"

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.

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)

About the Author

thomastn


Member

Location: United States United States

Other popular Windows Presentation Foundation articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 20 of 20 (Total in Forum: 20) (Refresh)FirstPrevNext
Questionadditional PinmemberComan Ovidiu2:39 24 Feb '10  
General[My vote of 1] Hey - what's this keyboard for? Pinmembertonyt17:24 21 Feb '10  
GeneralIssue with WPF AutoComplete TextBox with Chinese and Japanese inputs Pinmembersandyyyyyyyyyy0:35 19 Nov '09  
GeneralMy vote of 2 Pinmemberrealvasche8:36 23 Oct '09  
Questionchinese IME issue Pinmembernnnnimisha21:17 5 Aug '09  
QuestionHow can I change the Background Color of the AutoComplete TextBox? Pinmembermicrosoft_kc21:59 23 Jun '09  
AnswerRe: How can I change the Background Color of the AutoComplete TextBox? PinmemberTrond Andersen5:08 22 Oct '09  
GeneralNice One Pinmemberkapil bhavsar0:49 8 May '09  
GeneralMy vote of 2 PinmemberRandomEngy8:44 14 Apr '09  
RantWait hold on... is your ComboBox hiding behind the text field? PinmemberMember 40300058:41 14 Apr '09  
GeneralThanks!! PinmemberDZaK8314:11 19 Mar '09  
GeneralAnother alternative Pinmembertoidy8:15 30 Sep '08  
Generalkeyboard oriented selection Pinmemberjohn c gibbons6:24 22 Sep '08  
GeneralNo keyboard navigation? Pinmemberarmentage5:59 17 Jul '08  
GeneralComboBox Selection PinmemberAndrewiski16:54 2 Jun '08  
GeneralRe: ComboBox Selection Pinmemberthomastn19:18 2 Jun '08  
GeneralRe: ComboBox Selection PinmemberAndrewiski11:24 5 Jun '08  
I came to the same conclusion before I made it back here to read your post. The only annoying thing I ran into was that the ComboBoxItem gets a dashed black border because they are infocus. I tried to remove the dashed border but was not able. I could change the comboboxitem border to red etc but the dashed border was always on top. Maying a solid black border with a thickness of one gave me the look I wanted and hide the dashed border. I also added a comboBox.Clear(); and textBox.Focus() to the ComboBox.SelectionChangedHandler(); as it to was leaving a funny dashed border after the selection was changed and focus was not returning to the textbox.

Also all you need on the downarrow is comboBox.Focus() as this will leave the user typed text in the textbox with out having to set the SelectedIndex.

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.

QuestionChanging data set? PinmemberJim Weiler11:32 31 May '08  
AnswerRe: Changing data set? [modified] Pinmemberthomastn18:53 2 Jun '08  
GeneralRe: Changing data set? Pinmemberboriska0022:39 15 Sep '09  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

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

PermaLink | Privacy | Terms of Use
Last Updated: 31 May 2008
Editor: Deeksha Shenoy
Copyright 2008 by thomastn
Everything else Copyright © CodeProject, 1999-2010
Web20 | Advertise on the Code Project