Click here to Skip to main content
Click here to Skip to main content
Go to top

Make similar suggestions for input text by remembering old inputs

, 15 Jul 2005
Rate this:
Please Sign up or sign in to vote.
Suggest most similar typed text in the past while typing new ones to guide the user to select previously typed texts.

Introduction

First of all, I apologize for my bad English...

A few days ago, I saw a question in a message board about how to suggest similar typed text below a control to guide the user to the previously typed texts. We have this ability in IE address bar.

This will show you previously typed similar addresses when you start typing the new address. To achieve this goal, I decided to make a new component. And here is the result of my effort Smile | :)

The control TST, which is exposed in this site, is used as a data structure for the inputs dictionary. So, you need to add Tst.dll as reference.

Using the code

Initializing Suggestor

Loading dictionary

You must load the dictionary before using it. The following method does this for you:

suggestor1.LoadFromFile("c:\\dic1.dic");

suggestor1 is an instance of the Suggestor component.

Binding Suggestor

After that bind the Suggestor control to your desired control (txtURL in my example) in this way:

suggestor1.BindToControl(txtURL);

OnSuggestDbClicked Event

This event is fired when the user double clicks any suggest which is shown to her; so that we can reflect appropriately to this if we catch this event in this way:

suggestor1.OnSuggestDbClicked+=
  new Suggestor.Suggestor.SuggestDbClicked(suggestor1_OnSuggestDbClicked);

Example:

We can do all the three steps in the following way:

private void Form1_Load(object sender, System.EventArgs e)
{
    try
    {
        suggestor1.LoadFromFile("c:\\dic1.dic");
    }
    catch
    {
        . . .
    }
    suggestor1.OnSuggestDbClicked+=
        new Suggestor.Suggestor.SuggestDbClicked(
                         suggestor1_OnSuggestDbClicked);
    suggestor1.BindToControl(txtURL);
    . . .
}

Remember that try...catch is important because for the first time when you do not have any file as dictionary, it will throw an exception!

Adding

At first, you just have an empty dictionary, which can be expanded by adding new strings to it in the following way:

suggestor1.AddToDictionary(txtURL.Text);

Suggesting

OK, here is the main goal of the component: to suggest similar words! Use this method in proper place to show suggestions:

suggestor1.Suggest(txtURL.Text);

Interact with Suggestor

Here you'll see how to navigate through all suggestions and how to obtain the selected suggestion.

To select the next suggestion call this:

suggestor1.SelectNext();

To select the previous suggestion call this:

suggestor1.Previous();

To obtain the selected suggestions:

string selected=suggestor1.GetSelected();

And to hide suggestions call this:

suggestor1.ClearSuggestions();

Finalizing Suggestor

You must save your dictionary to have access to it in future:

suggestor1.SaveInFile("c:\\dic1.dic");

Combined example

Here we will see an example for the address bar scenario where we have suggestor1 an instance of Suggestor, txtURL as the input textbox and btnBrowse as the submit button. frmMain is the main form. Here it is:

private void txtURL_KeyDown(object sender, 
               System.Windows.Forms.KeyEventArgs e)
{
    try
    {
        if(e.KeyCode==Keys.Down)
            suggestor1.SelectNext();
        if(e.KeyCode==Keys.Up)
            suggestor1.SelectPrevious();
        if(e.KeyCode==Keys.Enter)
            {
                txtURL.Text= suggestor1.GetSelected();
                suggestor1.ClearSuggestions();
            }
    }
    catch
    {
    }
}
private void frmMain_Closing(object sender, 
             System.ComponentModel.CancelEventArgs e)
{
    try
    {
        suggestor1.SaveInFile("c:\\dic1.dic");
    } 
    catch
    {
    }
}
private void txtURL_KeyUp(object sender, 
                     System.Windows.Forms.KeyEventArgs e)
{
    suggestor1.Suggest(txtURL.Text);
}
private void suggestor1_OnSuggestDbClicked()
{
    txtURL.Text=suggestor1.GetSelected();
    suggestor1.ClearSuggestions();
}
private void txtURL_TextChanged(object sender, System.EventArgs e)
{
    suggestor1.ClearSuggestions();
}
private void Form1_Load(object sender, System.EventArgs e)
{
    try
    {
        suggestor1.LoadFromFile("c:\\dic1.dic");
    }
    catch
    {
    }
    suggestor1.OnSuggestDbClicked+=
       new Suggestor.Suggestor.SuggestDbClicked(
                       suggestor1_OnSuggestDbClicked);
    suggestor1.BindToControl(txtURL);
    . . .
}

Also, there is another simple example of using this component (see at the top of the page).

Points of interest

I didn't write this article to catch anyone's attention, but to try and develop a useful component for being more familiar with component based Software Engineering. Please keep me posted about any bugs or suggestions.

Don't count your chicks before they hatch!

License

This article, along with any associated source code and files, is licensed under The Common Public License Version 1.0 (CPL)

Share

About the Author

EEmadzadeh
Software Developer (Senior)
United States United States
Perl, CGI, Linux, JavaScript
ASP.NET C#,J#,VB .NET
C/C++
hate VI,VIM but felt in love with Komodo-Edit recently :P

Comments and Discussions

 
GeneralDynamic AutoComplete Tool PinmemberAndrejV24-Jul-05 22:29 
GeneralTry/Catch PinmemberKidan20-Jul-05 6:17 
GeneralRe: Try/Catch PinmemberEEmadzadeh22-Jul-05 21:11 
thanks for you attention and nice suggest.
 
First :
i used try/catch not just for checking if file exist or not (any way this exception will be surely thrown in the first call)
but also for handling any other possible exception,if the file had problem in saving then in load method try to parse file as an TST it will throw exception(i used a TSTonDisk class,which i made 1 year ago,that throw these exceptions).
these exception can caused by trying to load a dictinary which was saved some null character or newline delimiter as an key in TST .
So catching these exceptions stops control from crashing and make it more stable.
 
Second :
at the end i think in normal input condition you are correct because we use try and catch when it is worth .
may be change it in next versionUnsure | :~
 

QuestionWhy not an ExtenderProvider ? Pinmembermcarbenay18-Jul-05 20:41 
AnswerRe: Why not an ExtenderProvider ? PinmemberEEmadzadeh22-Jul-05 20:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

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

| Advertise | Privacy | Mobile
Web02 | 2.8.140916.1 | Last Updated 16 Jul 2005
Article Copyright 2005 by EEmadzadeh
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid