Click here to Skip to main content
15,867,594 members
Articles / Programming Languages / C#
Article

Autocomplete Textbox Control

Rate me:
Please Sign up or sign in to vote.
1.95/5 (16 votes)
26 Jan 20042 min read 101K   2.5K   36   4
A textbox control that will complete what the user types. It allows the user to choose what to remember for autocompletion.

Introduction

This is a textbox control that will autocomplete what the user is typing in it. It uses a list of words that the user can create. It uses a text file to store the list. I have stored it this way to allow a user to easily edit the list with Notepad.

Background

I had a user who needed to enter data that was often the same, but every once in a while changed. The application consisted mostly of textboxes and the user wanted to keep it that way, instead of having some combo controls thrown in. Sometimes they wanted to be able to add an item to the list, such as a new company that they were dealing with.

I could have stored the data in the registry or a database, but decided on a text file to allow the user to easily edit it.

Using the code

Just add the control to the toolbox by browsing to the DLL. Then drag and drop the control to your form. The code consists of one class, senseControl2, which inherits from TextBox. I have added a few properties to the TextBox control. The ChoiceFile property lets you name a file to store the choices in. You can use the same file for more than one control and keep them in sync with the ChoicesSaved event. The ChoicesSaved event is raised whenever the user saves a new item. You can use code similar to this in the event handler:

C#
private void senseControl_ChoicesSaved(object sender, System.EventArgs e)
{
    if(sender == this.senseControl1)
        this.senseControl2.ReloadChoices();
    else
        this.senseControl1.ReloadChoices();
}

You do not need to assign a filename, the control will default to the control's name with a .txt suffix. Also you may pre-set some values by using the Choices property. This will create a text file with these items, the first time a user saves a new item.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
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

 
GeneralMultiLine Bug [modified] Pin
MANSATAN14-Aug-06 22:42
MANSATAN14-Aug-06 22:42 
QuestionHow i can make it work when close the Form Pin
M.F2-Jul-06 14:43
M.F2-Jul-06 14:43 
GeneralDemo application not working Pin
Drew Noakes30-Mar-05 2:59
Drew Noakes30-Mar-05 2:59 
GeneralEvents not firing Pin
Anonymous16-Jul-04 7:48
Anonymous16-Jul-04 7:48 

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.