.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> Windows Application >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Search Engine - Windows Tool using C#

Posted By:Mahadesh Mahalingappa       Posted Date: August 06, 2011    Points: 200    Category: Windows Application    URL: http://www.dotnetspark.com  

In this article we will create simple search engine in windows application using C# code.
 

Objective :

I have always felt the need if I could 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 in database and also Search them using the tool.  Let's get started and 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)
);

Lets create a windows form project WinSearchEngine . I have created a Simple User Interface




Creating Data Model :

Lets add an Ado.net Entity Data Model. Lets name it as Dictionary.edmx  .





Lets 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 lets 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();
	
   // 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 :

 

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 lets 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);
            }

        }
Lets now give it a run and see how the code works .




Message is displayed as shown below :





I went and checked the database the entry was added .

So we have added an entry in the database . Now lets 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;
            Clipboard.SetText(selectedDictionaryitem.site_adress);
        }

Now I should be able to copy the link and open it in the browser .


Ok so things working fine now. But still there is something missing .  AutoComplete that is

it. Well lets 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 .  Lets give it a try :



Great it works .


Happy Coding.

 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend