Click here to Skip to main content
Click here to Skip to main content

Tagged as

Search Engine – Windows Tool using C#

, 16 Jul 2011
Rate this:
Please Sign up or sign in to vote.
A tool which would help us add topics and their corresponding sites in database and also search them using the tool.

Objective

I have always felt the need for a personal search engine like Google Smile | :) 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.

image001.jpg

Creating Data Model

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

image002.jpg

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:

image003.jpg

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:

image004.jpg

Message is displayed as shown below:

image005.jpg

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.

image006.jpg

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:

image007.jpg

Great, it works!

Happy coding!!

History

  • 16th July, 2011: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

No Biography provided

Comments and Discussions

 
GeneralMy vote of 1 Pinmembermariuszkiler23-Apr-14 1:52 
GeneralMy vote of 1 PinmemberFederico Sasso17-Jul-11 21:31 
SuggestionThis is something like bookmarking software PinmemberRavimal Bandara16-Jul-11 23:41 
QuestionNot a search engine PinmemberSean Hederman16-Jul-11 20:48 
Calling this a search engine is like calling "Hello World" an enterprise application. You don't have parser or stemmer. You're relying on the DB for lookups, and you don't even have an index. You don't handle multiple terms pointing to the same resource well.
 
This is called a Lookup Table.

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
Web01 | 2.8.140827.1 | Last Updated 16 Jul 2011
Article Copyright 2011 by Mahadesh Mahalingappa
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid