C# WebBrowser Control (Navigate Method)

Use the WebBrowser control in Windows Forms. Call the Navigate method.

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

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.

Info: As with other controls, the WebBrowser offers event handlers. These trigger when a page is being loaded and when the page is loaded.

Tip: You can add the event handlers in the above code example by double-clicking on the Form itself to create the Form1_Load handler.

And: To create Navigating and DocumentCompleted, right-click on the WebBrowser, select properties, and then click on the lightning bolt.

C# program that shows WebBrowser event handlers 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(""); } 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"; } } }

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.

And: After the website finishes loading, you will see the title bar changes to indicate the site loaded. DocumentCompleted 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, users can take unexpected actions, like navigate to any page.

Navigate. There are multiple overloaded versions of Navigate. 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.

Note: For places with slow connections or slow remote applications, this event handler is useful for providing feedback.

DocumentCompleted. 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.

And: At this point, you can indicate completion through a change in the window's title or another user interface element.

Set Url. 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

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? 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 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.
Dot Net Perls
© 2007-2020 Sam Allen. Every person is special and unique. Send bug reports to