C# WebBrowser

WebBrowser provides an Internet Explorer control. In Windows Forms, you can add a control that uses Internet Explorer to display web pages. This control is called the WebBrowser control. It has useful options you can add to create a kiosk-like browser with many limitations.

WebBrowser control

Start

Note

To begin, let's note some important parts of the WebBrowser control. The control offers the Navigate method, which gives you a lot of options for changing the location of the currently viewed page.

You can also set the Url property to change the current page. As with other controls, the WebBrowser offers event handlers. These trigger when a page is being loaded and when the page is loaded.

Program that shows WebBrowser event handlers: C#

using System;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
	public Form1()
	{
	    InitializeComponent();
	}

	private void Form1_Load(object sender, EventArgs e)
	{
	    // When the form loads, open this web page.
	    webBrowser1.Navigate("http://www.dotnetperls.com/");
	}

	private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
	{
	    // Set text while the page has not yet loaded.
	    this.Text = "Navigating";
	}

	private void webBrowser1_DocumentCompleted(object sender,
	    WebBrowserDocumentCompletedEventArgs e)
	{
	    // Better use the e parameter to get the url.
	    // ... This makes the method more generic and reusable.
	    this.Text = e.Url.ToString() + " loaded";
	}
    }
}

Program result

See screenshot at top of article.
The C# programming language

You can add the event handlers in the above code example by double-clicking on the Form itself to create the Form1_Load handler. To create webBrowser1_Navigating and webBrowser1_DocumentCompleted, please right-click on the WebBrowser control and select properties, and then click on the lightning bolt.

When you execute this program, you will find that the text "Navigating" immediately appears in the title bar. This is because the Load event is triggered, and the Navigating event is then triggered. After the website finishes loading, you will see the title bar changes to indicate the site loaded. This is because the DocumentCompleted event handler was triggered.

Context menus

By default, you will get the context menu for Internet Explorer when you right-click on the content area of the WebBrowser. If you are making a kiosk-like browser application, you probably won't want this. It could be confusing.

Tip:To disable this option, please set the IsWebBrowserContextMenuEnabled property to False in the Properties window.

Note:If you have the default browser context menu available, people may be able to take actions you do not want them to, like navigate to any page.

Navigate

Method call

The Navigate method in the WebBrowser is an important part of the control. There are multiple overloaded versions of this method. You can pass it a Uri object, a string url, a bool that tells whether to load in a new window, and even a byte array indicating what data to POST to the server through HTTP.

Navigating:With the Navigating event handler, you can execute code whenever a page has begun loading but has not finished. For places with slow connections or slow remote applications, this event handler is useful for providing feedback.

DocumentCompleted

Lightning bolt

The DocumentCompleted event handler is probably the most useful event handler on the WebBrowser. The code you put in this handler is executed whenever the page you navigate to is finished loading. At this point, you can indicate completion through a change in the window's title or another user interface element.

Set Url

Uri type

Instead of calling the Navigate method, you can actually just set the Url property of the WebBrowser to change the current page shown. You will need to use the "new Uri()" constructor syntax when you assign this property.

Uri Class

Scroll bars

In some kinds of kiosk-style web browser applications, you may want to hide the scroll bars on the WebBrowser.
If your user will not have a mouse,
scroll bars will not be useful,
after all.

Tip:To hide the scroll bars, please set the ScrollBarEnabled property to False.

Url bar?

Question and answer

The typical tutorial for the WebBrowser control shows you how to add a URL bar, and back and forward buttons. To implement these options, you could simply add a TextBox. And when enter is pressed, you could call the Navigate method.

Tip:The back and forward buttons can simply call the webBrowser1.GoForward() and webBrowser1.GoBack() methods.

Summary

The WebBrowser control in Windows Forms invokes an inline instance of the Internet Explorer web browsing engine. Useful for providing a kiosk-style web browsing function, this control gives you the ability to handle local and remote web pages with ease.


C#: Windows