C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
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("http://www.dotnetCodex.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";
}
}
}
And: After the website finishes loading, you will see the title bar changes to indicate the site loaded. DocumentCompleted was triggered.
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.
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.
And: At this point, you can indicate completion through a change in the window's title or another user interface element.
Tip: To hide the scroll bars, please set the ScrollBarEnabled property to False.
Tip: The back and forward buttons can simply call the webBrowser1.GoForward() and webBrowser1.GoBack() methods.