Search Engine – Windows Tool using C#
Objective
I have always felt the need for a personal search engine like Google :) which would make life easy for me. Here I am creating a tool which would help us add topics and their corresponding sites in database and also search them using the tool. Let’s get started and design the search engine.
Create a table in the database:
create table WinSearchEngine_Dictionary
( id integer identity primary key,
topic varchar(200),
site_adress varchar(200)
);
Let's create a Windows Form project WinSearchEngine
. I have created a Simple User Interface.

Creating Data Model
Let's add an ADO.NET Entity Data Model. Let's name it as Dictionary.edmx.

Let's go ahead and create a dictionary
class. This is static
class which provides me with the methods I need to populate data. I have used Linq with Lambda Expressions.
I have created a simple method getData
. This is how the method looks like:
public static IEnumerable getData(string topic)
{
// create the context object to access the Entities
SearchEngineEntities context = new SearchEngineEntities();
// returning the Ienumerable data where topic starts with
// string parameter topic passed
return context.WinSearchEngine_Dictionary.Where(x => x.topic.StartsWith(topic))
.Select (x => x);
}
Inside the Search Button click, let's binds the resultset
to the ListBox
. The display member is site_adress
.
private void Search_Click(object sender, EventArgs e)
{
listBox1.DataSource = Dictionary.getData(textBox1.Text);
listBox1.DisplayMember = "site_adress";
}
OK, we are done with the search logic.
Now, I create another method in the Dictionary
class.
public static void addData(string topic, string siteaddress)
{
// create the context object to access the Entities
SearchEngineEntities context = new SearchEngineEntities();
// Create a new entity of the Type WinSearchEngine_Dictionary
var newSearchEntity = new WinSearchEngine_Dictionary();
// Let's set the values of the entity
newSearchEntity.topic = topic;
newSearchEntity.site_adress = siteaddress;
// Add the object to the context
context.AddObject("WinSearchEngine_Dictionary", newSearchEntity);
// Let’s Save the context
context.SaveChanges();
}
I then create a new form named ChildWindow
. The UI looks as below:

I then go back to the Form1
and in the Add Button click, place the following code to display the ChildWindow
.
private void button1_Click(object sender, EventArgs e)
{
ChildWindow form2 = new ChildWindow();
form2.Activate();
form2.Show();
}
Now let's get to the Childwindow
form.
private void button1_Click(object sender, EventArgs e)
{
try
{
Dictionary.addData(textBox1.Text, textBox2.Text);
MessageBox.Show("Entries have been added to the Database");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Let's now give it a run and see how the code works:

Message is displayed as shown below:

I went and checked the database that the entry was added.
So we have added an entry in the database. Now let's try searching for it from our tool.

What I also want is to be able to copy the link and open it in the browser.
So for the ListBox
click, I add the following code:
private void listBox1_MouseClick(object sender, MouseEventArgs e)
{
var selectedDictionaryitem = listBox1.Items[listBox1.SelectedIndex]
as WinSearchEngine_Dictionary;
Clipboard.SetText(selectedDictionaryitem.site_adress);
}
Now, I should be able to copy the link and open it in the browser.
Ok so things are working fine now. But still, there is something missing. AutoComplete
that is it. Well let's add it then.
To add AutoComplete
, I will create a AutoCompleteStringCollection
as shown below:
AutoCompleteStringCollection topicsCollection = new AutoCompleteStringCollection();
Let me add another method in the Dictionary
class.
public static IList gettopics()
{
SearchEngineEntities context = new SearchEngineEntities();
return context.WinSearchEngine_Dictionary.Select(x => x.topic).ToList();
}
As can be seen, this method will list me all the topics.
Now I go ahead and add the following code in the TextBox
change event of Form1
which displays the topics.
private void textBox1_TextChanged(object sender, EventArgs e)
{
// Dictionary.gettopics give me the list of topics.
// I loop through the resultset and add it to the topicsCollection
foreach(string str in Dictionary.gettopics())
{
topicsCollection.Add(str);
}
textBox1.AutoCompleteMode = AutoCompleteMode.Suggest;
textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
// Assign the topicsCollection to the textBox1 AutoCompleteCustomSource
textBox1.AutoCompleteCustomSource = topicsCollection;
}
OK, so we are done now. Things should work now. Let's give it a try:

Great, it works!
Happy coding!!
History
- 16th July, 2011: Initial post
Post Comment
ITetDn I truly appreciate this article post. Cool.
zCl9PQ I really liked your blog.Really thank you! Fantastic.
WHi0qD Thanks so much for the blog post.Really thank you! Keep writing.
4hBc68 I am so grateful for your article.Thanks Again. Really Great.