Search Engine - Windows Tool Using C#

Objective :

I have always felt the need to have 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 to a database and also search them using the tool. Let's get started designing 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 Dictionary.edmx.


Let's go ahead and create a dictionary class. This is a static class provides the methods I need to populate data. I have used LINQ with Lambda Expressions.

I have created a simple method getData. This is what 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 bind 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();
    // Lets 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

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 handler add the following code to display the ChildWindow:

private void button1_Click(object sender, EventArgs e)
    ChildWindow form2 = new ChildWindow();

Now let's get to the Childwindow form.

private void button1_Click(object sender, EventArgs e)
        Dictionary.addData(textBox1.Text, textBox2.Text);
        MessageBox.Show("Entries have been added to the Database");
    catch (Exception ex)

Let's now give it a run and see how the code works.


A message is displayed as shown below:


I went and checked the database; the entry was added.

So we have added an entry to the database. Now let's try searching it from our tool


What I also want is to be able to copy the link and be able to 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;

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())
    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.