WebBrowser ControlUse the WebBrowser control in Windows Forms. Call the Navigate method.
This page was last reviewed on Oct 1, 2022.
WebBrowser. This control can be used to display web pages. It has many useful options. You can use this Windows Forms control with C# code to create a kiosk-like browser.
Getting started. To begin, drag a WebBrowser to your Windows Form in Visual Studio, and add a Navigating event. The Load event on the form can be used to initialize WebBrowser.
Navigate example. The control offers the Navigate() method. This gives many 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 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.
Result When you execute this program, you will find that the text "Navigating" immediately appears in the title bar.
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"; } } }
Context menus. By default, you will get the context menu for when you right-click on the content area of the WebBrowser. This can be disabled with a property.
Tip To disable this option, please set the IsWebBrowserContextMenuEnabled property to False in the Properties window.
Warning If you have the default browser context menu available, users can take unexpected actions, like navigate to any page.
DocumentCompleted. This event handler is often useful. 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 just set the Url property of the WebBrowser. You will need to use the Uri() constructor syntax when you assign this property.
Summary. The WebBrowser control supports web browsing. 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 is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Oct 1, 2022 (simplify).
© 2007-2022 sam allen.
see site info on the changelog.