Monday, July 28, 2014

.Net Framework Web Browser Class


Web Browser class is one of the most important class provided by .NET Framework for tasks related with web. It has lots of methods which can be used to implement web related tasks such as web scraping, creating a inbuilt browser in a software and many more.

Now I am going to let you know how to create program which will search a given word in yahoo search engine using this WebBrowser class. The language I am using to explain is C# here but you can use languages as visual C++, F#, VB as well.
You need to have some knowledge about C# and HTML before begin to do this task because almost all the things are represented in HTML in a web page thus you need to have an  understanding about HTML. Also if you do not know anything about C# then you are again in trouble. Therefore the prerequisites are C# (I am using Visual Studio 2013 IDE)  and HTML.

Lets get into work,

open visual studio and go to New => project => select windows forms Application under C#

Then you will see a windows form appear with title Form1. Now drag  a "button", "textbox" and a "web browser" from the tool box.


 Set the URL of the web browser as https://www.yahoo.com and text of the button(not the name) as "Go".
you have to make sure that the names of each component were not changed.
web browser name  : webBrowser1
button name            : button1
text box name         : textBox1

Now click on the text box and set TextChanged event as shown below:


just double clik on the TexChanged event(highlighted in yellow).

then a new file will be opened(Form1.cs)  and you will see a method called "textBox1_TextChanged".

 Now always when we  type something on the text box, this method is invoked. What we expect from this is when we type something on the text box it should be appeared on the search box of the yahoo search engine. Now we place a code inside textBox1_TextChanged method to do this task.

How it looks like after adding the code

The piece of code we used it shown above. Now your basic HTML and C# knowledge will help you to understand what happened and I will explain how we got the element ID.(p_13838465-p).

go to the shown link using your web browser. https://www.yahoo.com

now right click on the search box and click Inspect Element (I assume you know these things)

Finding ID of the search box

Then you will see the ID of the search box.

Our next step is setting the "go" button in the windows form work as "search web" button in the web page.

set button click event handler on the "go" button or just double click on the "go" button to set it automatically. set following code to the button1_Click method.

You can find the ID of the search button by the same method used to find the ID of search box.


This is all with coding and now you can test your program by running the code.

When you run the program you will see the web page is loaded into the web browser window.

Then type anything in the text box of the program, then you will see same text appears on the search box too.

When you click the "go" button it will search the web with the entered word.

I hope now you got an idea about some of usages of the web browser class. Like this you can do lots of advanced things with using this class.

Comment below If you have any question regarding this program.