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

Make similar suggestions for input text by remembering old inputs

Rate me:
Please Sign up or sign in to vote.
3.24/5 (6 votes)
15 Jul 2005CPL2 min read 29.7K   433   11   5
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 :-)

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:

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

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

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

Example:

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

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

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

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

C#
suggestor1.SelectNext();

To select the previous suggestion call this:

C#
suggestor1.Previous();

To obtain the selected suggestions:

C#
string selected=suggestor1.GetSelected();

And to hide suggestions call this:

C#
suggestor1.ClearSuggestions();

Finalizing Suggestor

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

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

C#
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
    {
    }
}
C#
private void frmMain_Closing(object sender, 
             System.ComponentModel.CancelEventArgs e)
{
    try
    {
        suggestor1.SaveInFile("c:\\dic1.dic");
    } 
    catch
    {
    }
}
C#
private void txtURL_KeyUp(object sender, 
                     System.Windows.Forms.KeyEventArgs e)
{
    suggestor1.Suggest(txtURL.Text);
}
C#
private void suggestor1_OnSuggestDbClicked()
{
    txtURL.Text=suggestor1.GetSelected();
    suggestor1.ClearSuggestions();
}
C#
private void txtURL_TextChanged(object sender, System.EventArgs e)
{
    suggestor1.ClearSuggestions();
}
C#
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)


Written By
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 Pin
AndrejV24-Jul-05 22:29
AndrejV24-Jul-05 22:29 
GeneralTry/Catch Pin
Kidan20-Jul-05 6:17
Kidan20-Jul-05 6:17 
GeneralRe: Try/Catch Pin
EEmadzadeh22-Jul-05 21:11
EEmadzadeh22-Jul-05 21:11 
QuestionWhy not an ExtenderProvider ? Pin
mcarbenay18-Jul-05 20:41
mcarbenay18-Jul-05 20:41 
AnswerRe: Why not an ExtenderProvider ? Pin
EEmadzadeh22-Jul-05 20:52
EEmadzadeh22-Jul-05 20:52 

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.