Best Industrial Training in C,C++,PHP,Dot Net,Java in Jalandhar

Sunday 15 January 2012

How to make a Web Browser in C # 2010

Drag the following controls to the form:
A webbrowser, a textbox, and 6 buttons:
Organize the controls on the form like shown in the picture below.
Now we will add the codes to the buttons: Double click on each button before adding the code for each of them:
Go Button: double click then add the following highlighted code:
private void button1_Click(object sender, EventArgs e)
{
//navigate the webbrower to the url typed in textBox1
webBrowser1.Navigate(textBox1.Text);
}

Home Button: double click then add the following highlighted code:
private void button4_Click(object sender, EventArgs e)
{
//Navigate the browser to the home page - in this case it is google.com
webBrowser1.Navigate("www.google.com");
}

Refresh Button: double click then add the following highlighted code:
private void button2_Click(object sender, EventArgs e)
{
//Refreshes the page
webBrowser1.Refresh();
}

Stop Button: double click then add the following highlighted code:
private void button3_Click(object sender, EventArgs e)
{
// Stops the browser navigating
webBrowser1.Stop();
}

Back Button: double click then add the following highlighted code:
private void button5_Click(object sender, EventArgs e)
{
//The browser will go back to the last navigated page if Avalaible
webBrowser1.GoBack();
}

Forward Button: double click then add the following highlighted code:
private void button6_Click(object sender, EventArgs e)
{
webBrowser1.GoForward();
}

We will now add the last piece of code to the project, the following code will return the page url to the text box after navigating is completed.
Document Completed event: add the following hightlighted code
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
textBox1.Text = webBrowser1.Url.ToString();
}

No comments:

Post a Comment