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:
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!