Updated date:

Walk-Through: Create an Auto Complete Text Box in C#

Author:

I am a software engineer. I have been working with C++, MFC, and .net technologies for 15 years. I like video games and reading books.

1. Introduction

The auto complete feature of a text box allows the user to enter few letters of the text in it and completes the remaining automatically. Say, for example, a country text box, which will fill the entry India when first two letters are typed. There is two important ways we can save the typing. One is auto complete by filling the remaining text and another one is providing a suggestion in the form a matching list and pick a correct one from it.

In this article, we will design the "auto complete" text box with above said two kinds of an auto complete feature. This article uses only form designer and there is no added.

2. Form Design

Create a Visual C# Windows application using VS2005. Add two label and two text boxes. Use the following picture for reference:

Auto Complete Text Box Form Design

Auto Complete Text Box Form Design

2.1 Autocomplete with Append Mode

We will set below properties for the First Text box Control:

  1. Set CustomSource value for the property Autocompletesource
  2. Set Append value for the property AutoCompleteMode
  3. Set the below-specified value for the Property AutoCompleteCustomSource

Mahesh Chand

Sivaraman Dhamodaran

Praveen Kumar

Hashit viyas

Dentin Joy

Suthish Nair

Sure Meenakshi

Mike Gold

The CustomSource value property for the Autocompletesource indicates that we will supply the Data for the Auto Completion to work. The below picture shows how we give list of values as collection:


AutoCompleteSource Property

AutoCompleteSource Property

The property Append Mode specifies to auto complete the text after typing few letters. For example, let us consider user typed the letter M. The Text Box auto completes with the name “Mahesh Chand”. There are two names called “Mahesh Chand” and “Mike Gold”. The auto complete is done based on the alphabetical order and hence Mahesh Chand is displayed on the text box. Now, when we type the next letter as ‘i’, the auto complete changes the text “Mike Gold”.

2.2 Auto Complete with Suggest Mode

For the second Text Box, set the below given properties:

  1. Set CustomSource value for the property Autocompletesource
  2. Set Suggest value for the property AutoCompleteMode
  3. Set the below-specified value for the Property AutoCompleteCustomSource

The only difference here is that we set Suggest value for the property AutoCompleteMode.

Unlike append, Suggest Mode will show all the options. For Example, let us say; user type the Letter M in the second text box. The form will immediately show two possible options Mike Gold and Mahesh Chand as Pick list. User can pick a suggested and it will get filled in the text box.

3. Run the Application and Test

There is no code we need to write. The properties we set is enough to achieve the required application behavior.

  1. Compile and then run the Application.
  2. In the First text box type only the letter S

Note that the auto complete (Append) tries to fill the text box value based on the alphabetical order. The screenshot is below:

Auto Complete TextBox Append Mode

Auto Complete TextBox Append Mode

Let us say we want to Type Suthish Nair in the text box. But, when we type the letter ‘u’, the text box appends or auto fills it with the name “Sure Meenakshi”. This is because, the name precedes Suthish Nair alphabetically. Once we type the letter ‘t’, we get the required auto complete.

Now, we will type 'S' in the Suggest Mode text box. Unlike, Append Mode, the Suggest Mode text box displays all the possible options like a pick list. We can select the third one from the list as we need to get Suthish Nair in the text box. Below is the screenshot:

Auto Complete TextBox Suggest Mode

Auto Complete TextBox Suggest Mode

Example Project: Download

© 2018 sirama