Working with web browser control in C#

Abhisek
Posted by Abhisek under C# category on | Views : 4571
Working with web browser control in C#

****************************************

The System.Winsows.Forms.WebBrowser control is new to the .NET 2.0 and is a highly configurable mini web browser that may be embedded into any window form. This control defines a Url property that can be set to any valid Url, formally represented by the System.Uri type.

First create a new Window form application in visual studio in C#. In the form add a WebBrowser control a label(lblUrl), a TextBox(txtUrl) to enter the Url and a button(btnGo) to perform the HTTP request. You can set different properties of WebBrowser control in the properties window. For now just change the Url property to"http://www.dotnetfunda.com" so that when we run the application the webpage is displayed. Change the label's text property to "Enter URL". Now add the following code in the Click event handler of Go button.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Windows.Forms;

namespace WebBrowser
{
public partial class WebBrowserUse : Form
{
private void btnGO_Click(object sender, EventArgs e)
{
// Set URL based on value in the TextBox control.
myWebBrowser.Url = new System.Uri(txtUrl.Text);
}
}

}


Now run the application and you will see that the webbrowser control will display our DotNetFunda homepage. You can also enter other Url. But you have to specify the particular Url, i.e, "http://www.website.domain", otherwise it will not be able to show the page.

Comments or Responses

Login to post response