SIGN UP MEMBER LOGIN:    
ARTICLE

WebBrowser Control in C# and Windows Forms

Posted by Mahesh Chand Articles | Windows Forms C# March 09, 2011
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.
Reader Level:
Download Files:
 

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 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 a WebBrowser controls 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 form 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 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.

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 Navigate method.

// GO button click event handler.

private void GoButton_Click(object sender, EventArgs e)
{
    if (String.IsNullOrEmpty(UrlTextBox.Text) || UrlTextBox.Text.Equals("about:blank"))
    {
        MessageBox.Show("Enter a valid URL.");
        UrlTextBox.Focus();
        return;
    }
    OpenURLInBrowser(UrlTextBox.Text);       
}

private void OpenURLInBrowser(string url)
{       

    if (!url.StartsWith("http://") && !url.StartsWith("https://"))
    {
        url = "http://" + url;
    }

    try
   
{
        webBrowser1.Navigate(new Uri(url));
    }
    catch (System.UriFormatException)
    {
        return;
    }
}

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.

// Home button takes user home

private void HomeButton_Click(object sender, EventArgs e)
{
    webBrowser1.GoHome();
}

// Go back
private
void BackButton_Click(object sender, EventArgs e)
{
    if (webBrowser1.CanGoBack)
        webBrowser1.GoBack();
}

// Next

private void NextButton_Click(object sender, EventArgs e)
{

    if (webBrowser1.CanGoForward)
        webBrowser1.GoForward();
}      

// Refresh

private void RefreshButton_Click(object sender, EventArgs e)
{
    webBrowser1.Refresh();
}

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.

// Save button launches SaveAs dialog

private void SaveButton_Click(object sender, EventArgs e)
{
    webBrowser1.ShowSaveAsDialog();
}

// PrintPreview button launches PrintPreview dialog

private void PrintPreviewButton_Click(object sender, EventArgs e)
{
    webBrowser1.ShowPrintPreviewDialog();
}

// Show Print dialog

private void PrintButton_Click(object sender, EventArgs e)
{
    webBrowser1.ShowPrintDialog();
}

// Properties button

private void PropertiesButton_Click(object sender, EventArgs e)
{
    webBrowser1.ShowPropertiesDialog();
}

 

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.

Web Browser control in WPF by Dhananjay Kumar on Aug 03, 2009
Web Browser in C# by Leo Koach on Apr 25, 2010
Web Browser in C# by Kapil Soni on Apr 14, 2009
How to use Web Browser control in Visual Studio 2005 by Chitkaran Singh on Feb 29, 2008
Web Browser in C# and VB.NET by Mahesh Chand on Aug 30, 2006
WebBrowser Control in WPF by Rahul Kumar Saxena on Apr 12, 2010
Windows Forms WebBrowser Control by Mahesh Chand on Feb 23, 2006
Web Enabled C# Application by Kamran on Jun 24, 2009

 

 

Login to add your contents and source code to this article
share this article :
post comment
 

Once web page is opened through web browser how to edit that page? I mean i want insert one button on click of that button new window should open,,Please reply.... Thanks,

Posted by Taraspreet Sing Brar Feb 01, 2012

The question concerns how the webbrowser post messages regarding 'what to do' when an item is clicked inside of its window. For instance, click a folder and you navigate to that folder, or click a link and you navigate to that link. You can override these methods. But if you click a file, that file is opened. What the heck do you override on that one? What is the message that is being sent? Is it the webbrowser class that should be looked at, or something else? I can't figure it out yet.

Posted by J R Oct 07, 2011

I think that the question of intercepting a file open is not directly a WebBrowser question. I think it is a more general Windows question. I am not sure however what the question is so it would be better to ask in one of the forums in this web site.

Posted by Sam Hobbs Oct 06, 2011

If I use the webbrowser class to browse my local drive and select some random text file to open. It normally pops open in Notepad or something that it is mapped to. How do I intercept this call and do something simple like a MessageBox.Show("Don't open that file.") and return without the actual file being opened?

Posted by J R Oct 06, 2011

I been looking for a sample to show me how to use the toolstrip progessbar1 and a web browser control. and the samples I did fine don't work. I wanted the tool strip progress bar to show the progress of the page being loaded. Can some one, any one make a sample for me and every one else would like that sample too.

Posted by Joe Sep 14, 2011
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Nevron Gauge for SharePoint
Become a Sponsor