SIGN UP MEMBER LOGIN:    
ARTICLE

Search Engine - Windows Tool Using C#

Posted by Mahadesh Mahalingappa Articles | C# Language July 17, 2011
Here I am creating a tool which would help us add topics and their corresponding sites in a database and also search them using the tool.
Reader Level:
Download Files:
 

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:

SercEngi1.gif

Creating Data Model :

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

SercEngi2.gif

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
    context.SaveChanges();
}

I then create a new form named ChildWindow. The UI looks as below:

SercEngi3.gif

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

SercEngi4.gif

A message is displayed as shown below:

SercEngi5.gif

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

SercEngi6.gif

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

SercEngi7.gif

Great it works.

Happy Coding.

Login to add your contents and source code to this article
share this article :
post comment
 

great help

Posted by mohit bansal Jul 23, 2011

Helpful Article

Posted by Angelina Erin Jul 18, 2011

Thanks for sharing , its very useful article

Posted by Dinesh Beniwal Jul 17, 2011
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Team Foundation Server Hosting
Become a Sponsor