WebBrowser Control in C# and Windows Forms

WebBrowser Control

 
WebBrowser control allows developers to build Web browsing capability within Windows Forms applications. In this article, I will demonstrate how to use the WebBrowser control in a Windows Forms application using C# and Visual Studio 2010.
 
In 2001, I published an article Web Browser in C# and VB.NET that showed how to use the WebBrowser ActiveX control in a Windows Forms 1.0/1.1 application. Well, things have changed since then.
 
This article is written using Visual Studio 2010 and Windows Forms 4. If you do not have Visual Studio 2010, you may use Visual C# 2010 Express which is free to download from the MSDN website.
 
Creating a WebBrowser
 
Create a Windows Forms application using Visual Studio 2010 or Visual C# 2010 Express.
 
In this application, I am going to add a ToolStrip and WebBrowser control to the form. In my ToolStrip control, I add a Label, a TextBox, and a few Button controls with a few separators.
 
By the time we are done with the application, the final user interface will look like Figure 1.
 
WebBrowserImg1.gif
Figure 1
 
After your Toolbar looks like Figure 1, drag a WebBrowser control from Toolbox to the Form and resize and dock the control the way you like on the Form. I dock the WebBrowser control at the bottom of the window.
 
WebBrowserImg2.gif 
Figure 2
 
In the next step, I am going to set default properties of the WebBrowser control.
 
To do so, right-click on the WebBrowser control, and select Properties. This action launches the Properties window. Feel free to set any properties you like. The Url property represents the web page a WebBrowser displays. In Figure 3, I set http://www.c-sharpcorner.com as the default page to display in the browser.
 
WebBrowserImg3.gif
Figure 3
 
Navigation
 
The WebBrowser class in code behind is associated with the WebBrowser control. So when you drag and drop a WebBrowser control to the Form, a WebBrowser class instance is created in the code behind.
 
The Navigate method of the WebBrowser class is used to open a URL in the WebBrowser.
  1. webBrowser1.Navigate(new Uri(url));  
The following code snippet is code written on the Go button click event handler where I open a URL in the WebBrowser using the Navigate method.
  1. // GO button click event handler.  
  2.   
  3. private void GoButton_Click(object sender, EventArgs e)  
  4. {  
  5.     if (String.IsNullOrEmpty(UrlTextBox.Text) || UrlTextBox.Text.Equals("about:blank"))  
  6.     {  
  7.         MessageBox.Show("Enter a valid URL.");  
  8.         UrlTextBox.Focus();  
  9.         return;  
  10.     }  
  11.     OpenURLInBrowser(UrlTextBox.Text);          
  12. }   
  13.   
  14. private void OpenURLInBrowser(string url)  
  15. {          
  16.   
  17.     if (!url.StartsWith("http://") && !url.StartsWith("https://"))  
  18.     {  
  19.         url = "http://" + url;  
  20.     }  
  21.   
  22.     try  
  23.     {  
  24.         webBrowser1.Navigate(new Uri(url));  
  25.     }  
  26.     catch (System.UriFormatException)  
  27.     {  
  28.         return;  
  29.     }  
  30. }  
WebBrowser control also has built-in browser methods to go home, forward, backward, refresh, save, print and others.
 
The following code snippet shows how to use GoForeward, GoBack, GoHome, and Refresh methods.
  1. // Home button takes user home  
  2.   
  3. private void HomeButton_Click(object sender, EventArgs e)  
  4. {  
  5.     webBrowser1.GoHome();  
  6. }   
  7.   
  8. // Go back  
  9. private void BackButton_Click(object sender, EventArgs e)  
  10. {  
  11.     if (webBrowser1.CanGoBack)  
  12.         webBrowser1.GoBack();  
  13. }   
  14.   
  15. // Next  
  16.   
  17. private void NextButton_Click(object sender, EventArgs e)  
  18. {  
  19.   
  20.     if (webBrowser1.CanGoForward)  
  21.         webBrowser1.GoForward();  
  22. }         
  23.   
  24. // Refresh  
  25.   
  26. private void RefreshButton_Click(object sender, EventArgs e)  
  27. {  
  28.     webBrowser1.Refresh();  
  29. }  
The ShowSaveAsDialog, ShowPrintDialog, ShowPrintPreviewDialog, and ShowProperties methods are used to display SaveAs diaog, Print dialog, PrintPreview dialog, and Properties dialog respectively. The following code snippet shows how to call these methods.
  1. // Save button launches SaveAs dialog  
  2.   
  3. private void SaveButton_Click(object sender, EventArgs e)  
  4. {  
  5.     webBrowser1.ShowSaveAsDialog();  
  6. }   
  7.   
  8. // PrintPreview button launches PrintPreview dialog  
  9.   
  10. private void PrintPreviewButton_Click(object sender, EventArgs e)  
  11. {  
  12.     webBrowser1.ShowPrintPreviewDialog();  
  13. }   
  14.   
  15. // Show Print dialog  
  16.   
  17.   
  18. private void PrintButton_Click(object sender, EventArgs e)  
  19. {  
  20.     webBrowser1.ShowPrintDialog();  
  21. }  
  22.   
  23. // Properties button  
  24.   
  25. private void PropertiesButton_Click(object sender, EventArgs e)  
  26. {  
  27.     webBrowser1.ShowPropertiesDialog();  
  28. }  

Summary

 
In this article, I discussed how to use a WebBrowser control in Windows Forms at design-time as well as run-time. After that, we saw how to use various properties and methods of the WebBrowser control.
 
Recommended Readings
 
Here are some more articles on the WebBrowser control including different versions of Visual Studio and Windows Forms.


Similar Articles
Mindcracker
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.