Thursday, 27 June 2013

how to create web browser in c#

create a Menu bar in win-forms, so we can include any number of tabs when ever needed using Menu strip Option in toolbox. Then assign some functionality to it. It would look like this.
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
private void nEWToolStripMenuItem_Click(object sender, EventArgs e)

{

Form1 fm = new Form1(); fm.Show();

}
private void eXITToolStripMenuItem_Click(object sender, EventArgs e)

{

Application.Exit();

}
After creating a menu strip in the background, where I have added two menu items New and Exit,
  • New for creating a new browser window,
  • And, Exit for exiting the browser.
We will add few more items like we have to create a address bar using ComboBox,Go button,Back,Forward and web browser to the window.
Note: I am Using Combo box over here, it is required to alter the collections of combo box to the specified Url,you can also try with Textbox also.
So let’s start coding for web browser in Search Button. here I am just navigating to the url specified in the combo box.
Search Button :
?
1
2
3
4
5
6
7
private void SEARCH_Click(object sender, EventArgs e)

{

webBrowser1.Navigate(new Uri(comboBox1.SelectedItem.ToString())) ;

}
Here make sure combo box values is converted to string.
Back Button :
?
1
2
3
4
5
6
7
private void BACK_Click(object sender, EventArgs e)

{

webBrowser1.GoBack();

}
Forward Button :
?
1
2
3
4
5
6
7
private void FORWARD_Click(object sender, EventArgs e)

{

webBrowser1.GoForward();

}
It will look somewhat Like this on Completion.
Now let’s run the programming, It will look like this.

No comments:

Post a Comment