Click here to Skip to main content
11,789,706 members (59,264 online)
Click here to Skip to main content

Extended .NET 2.0 WebBrowser Control

, 29 Mar 2006 1.6M 30.1K 327
Rate this:
Please Sign up or sign in to vote.
Extending the .NET 2.0 WebBrowser control.

Sample Image


  1. Introduction
  2. The goals, challenges, and solutions
  3. Creating the extended WebBrowser component
  4. Using the component
  5. Conclusion

1: Introduction

.NET 2.0 has a new WebBrowser control in the System.Windows.Forms namespace. This control itself is very useful, but doesn't supply some events that might be needed in certain situations. This article explains how to extend the WebBrowser control and add functionality for things like pop-up blocking, script error handling, and handling new windows in a tabbed browser environment.

For extending the WebBrowser control, some features are not documented in the Help files of the .NET Framework. Not letting us be stopped by the "This method supports the .NET Framework infrastructure and is not intended to be used directly from your code." message, it is possible to create an object that implements IWebBrowser2 and use all the functionality of the underlying browser. Besides this, DWebBrowserEvents2 can be implemented for adding events to the control.

This article assumes that you have already some knowledge of the browser interfaces IWebBrowser2 and DWebBrowserEvents2. Some knowledge about COM Interop and interfaces is also required.

2: The goals, challenges, and solutions

The goals of this component are:

  • Handling script errors in a neat way
  • Blocking unwanted pop-ups
  • Enabling functionality for tabbed browsing or MDI browsing
  • Making sure that a window is closed when it is closed by script

This section explains the problems associated with the goals and their solutions, in a short form. The next section goes more into the coding details.

Handling Script Errors

The WebBrowser control has a ScriptErrorsSuppressed property... Setting this property to true does actually a bit more than it is supposed to. It not only disables the script error dialog, but also the dialog for logging on to a secure website with user certificates... What if we still want that functionality, or we would like to be notified when a script error has taken place, or we would like to know all the details about the script error?

The script error can be caught by the HtmlWindow.Error event. This event fires whenever a script error occurs, with all the details. The challenge is that HtmlWindow is to be accesed with the HtmlDocument object, which is not always available. HtmlDocument comes available as soon as the Navigated event is fired. But what if the user refreshes the browser with F5? Sorry, the Navigated event doesn't fire. After some testing, I found that the only reliable way was to use the DownloadComplete event, which is not part of the default WebBrowser control.


  1. Implement DWebBrowserEvents2
  2. Create a DownloadComplete event
  3. When DownloadComplete fires, subscribe to the HtmlWindow.Error event
  4. Use the error event for obtaining the script error information
  5. Set the Handled property to true to suppress the script error

Blocking unwanted pop-ups

Pop-ups are most of the time not very welcome, or could be inappropriate. To block these things, some additional information is needed. NewWindow3 gives this information when the user uses Windows XP SP2, or Windows 2003 SP1 or better. If this event is not fired, NewWindow2 takes its place. When NewWindow3 is fired, you can check:

  • If the user initiated the action that leads to the new window
  • If the user holds the override key (the Ctrl Key)
  • If it is a pop-up displayed because of a window that is closing
  • The URL that is going to be opened
  • And more...

Using NewWindow3 clearly is very interesting for this purpose. To use this event, DWebBrowserEvents2 needs to be implemented.


  1. Implement DWebBrowserEvents2
  2. Create a new event and a new event arguments class
  3. Launch this event with the appropriate information
  4. After the event is fired, see if the navigation needs to be canceled

Enabling functionality for tabbed browsing or MDI browsing

Tabbed browsing seems to gain popularity these days. For Internet Explorer 7, this is one of the new features. The challenge in tabbed browsing is that you need to create a window when this is needed by scripts or links. Besides this, window name resolution should work over multiple windows or tabs. (For example: <A href="http://SomeSite" target="SomeWindowName"/>) To achieve this, the automation object (called ppDisp in the NewWindowX event, and Application in the IWebBrowser2 interface) should be passed from the new browser back to the event. To get access to the Application property, it is needed to get a reference to the underlying IWebBrowser2 interface.


  1. Override AttachInterfaces and DetachInterfaces
  2. Store a reference to the IWebBrowser2 interface object
  3. Create a property called Application that exposes the Application property of the interface
  4. Implement the DWebBrowserEvents2 interface
  5. Listen for NewWindow2 and/or NewWindow3 events
  6. Whenever an event is raised, create a new instance of the browser control
  7. Assign the event parameter ppDisp to the Application property of the new instance

Making sure that a window is closed when it is closed by script

When you invoke window.close() in JScript, it looks like the WebBrowser control hangs. Somehow, it can't be used for navigation, nor can it be used for anything else. It would be nice if we know when this happens. There are several events that fire when this happens, but none of the events gives us actually the information needed. Overriding WndProc and seeing if the parent is notified that the browser is destroyed, is the only reliable solution. (If someone knows how to get WindowClosing to work, it would be nice here!)


  1. Override WndProc
  2. Check for the message WM_PARENTNOTIFY
  3. Check for the parameter WM_DESTROY
  4. If this is the case, fire a new event (this event is called Quit, in the example)

3: Creating the extended WebBrowser component

From the last section, we have seen that all of this basically comes down to two things:

  1. Implement an object of type IWebBrowser2, for obtaining the Application property of the browser
  2. Implement DWebBrowserEvents2 for firing events

Implementing IWebBrowser2

The WebBrowser control has two methods that are undocumented: AttachInterfaces() and DetachInterfaces(). These methods need to be used when you want to obtain a reference to the IWebBrowser2 interface.

/// <span class="code-SummaryComment"><summary>

Next, we can add the Application property.

/// <span class="code-SummaryComment"><summary>

This property can be used for creating a new window, and redirecting the browser to this new window, when a new window event is fired.

Implementing DWebBrowserEvents2

The following events are implemented in this sample:

  • NewWindow2 and NewWindow3 (for blocking pop-ups and creating new windows)
  • DownloadBegin and DownloadComplete (for handling script errors)
  • BeforeNavigate2 (if you want to see where you're going before even starting to get there)

To neatly implement DWebBrowserEvents2, it is best to create a privately nested class in the component. This way, all the events that are needed are on one place and easy to find. When we instantiate this class, we provide a reference to the caller, whose methods can be invoked for raising the events we need.

Events are not attached at component construction, but a bit later. There are two methods here that provide this and can be overridden. These are CreateSink() and DetachSink(). When adding this all up, we get something like this (note that some code has been cut for readability):

/// <span class="code-SummaryComment"><summary>

4: Using the component

In the last section, we created a new component. Now, it's time to use the new events and get the maximum functionality out of the browser. For each of the goals, the details are explained here.

Handling the script errors

In the sample application, there is a tool window that simply shows a list of errors that occured, with their details. A single-instance class holds the script errors' information and notifies the subscribers when this information has been changed. For handling these script errors, the BrowserControl first attaches to the DownloadComplete event, and next subscribes to the HtmlWindow.Error event. When this event is fired, we register the script error and set the Handled property to true.

public partial class BrowserControl : UserControl
  public BrowserControl()
    _browser = new ExtendedWebBrowser();
    _browser.Dock = DockStyle.Fill;

    // Here's the new DownloadComplete event
    _browser.DownloadComplete +=
      new EventHandler(_browser_DownloadComplete);
    // Some more code here...
    // Some more code here...

  void _browser_DownloadComplete(object sender, EventArgs e)
    // Check wheter the document is available (it should be)
    if (this.WebBrowser.Document != null)
      // Subscribe to the Error event
      this.WebBrowser.Document.Window.Error +=
        new HtmlElementErrorEventHandler(Window_Error);

  void Window_Error(object sender, HtmlElementErrorEventArgs e)
    // We got a script error, record it
                             e.Description, e.LineNumber);
    // Let the browser know we handled this error.
    e.Handled = true;

  // Some more code here

Blocking unwanted pop-ups, and enabling functionality for tabbed browsing or MDI browsing

Handling pop-ups should be user configurable. For the purpose of demonstration, I've implemented four levels, ranging from blocking nothing to blocking every new window. This code is part of the BrowserControl, and shows how to do this. After the new window is allowed, the example shows how to let the new browser participate in the window name resolution.

void _browser_StartNewWindow(object sender, 
           BrowserExtendedNavigatingEventArgs e)
  // Here we do the pop-up blocker work

  // Note that in Windows 2000 or lower this event will fire, but the
  // event arguments will not contain any useful information
  // for blocking pop-ups.

  // There are 4 filter levels.
  // None: Allow all pop-ups
  // Low: Allow pop-ups from secure sites
  // Medium: Block most pop-ups
  // High: Block all pop-ups (Use Ctrl to override)

  // We need the instance of the main form,
  // because this holds the instance
  // to the WindowManager.
  MainForm mf = GetMainFormFromControl(sender as Control);
  if (mf == null)

  // Allow a popup when there is no information
  // available or when the Ctrl key is pressed
  bool allowPopup = (e.NavigationContext == UrlContext.None) 
       || ((e.NavigationContext & 
       UrlContext.OverrideKey) == UrlContext.OverrideKey);

  if (!allowPopup)
    // Give None, Low & Medium still a chance.
    switch (SettingsHelper.Current.FilterLevel)
      case PopupBlockerFilterLevel.None:
        allowPopup = true;
      case PopupBlockerFilterLevel.Low:
        // See if this is a secure site
        if (this.WebBrowser.EncryptionLevel != 
          allowPopup = true;
          // Not a secure site, handle this like the medium filter
          goto case PopupBlockerFilterLevel.Medium;
      case PopupBlockerFilterLevel.Medium:
        // This is the most dificult one.
        // Only when the user first inited
        // and the new window is user inited
        if ((e.NavigationContext & UrlContext.UserFirstInited) 
             == UrlContext.UserFirstInited && 
             (e.NavigationContext & UrlContext.UserInited) 
             == UrlContext.UserInited)
          allowPopup = true;

  if (allowPopup)
    // Check wheter it's a HTML dialog box.
    // If so, allow the popup but do not open a new tab
    if (!((e.NavigationContext & 
           UrlContext.HtmlDialog) == UrlContext.HtmlDialog))
      ExtendedWebBrowser ewb = mf.WindowManager.New(false);
      // The (in)famous application object
      e.AutomationObject = ewb.Application;
    // Here you could notify the user that the pop-up was blocked
    e.Cancel = true;

The reason the event is called StartNewWindow is that the code design guidelines do not allow an event to begin with "Before" or "After". "NewWindowing" doesn't have the same kind of ring to it Smile | :)

Using the Quit event

When the Quit event is fired, it's simply a matter of finding the right window or tab to close, and Dispose the instance.

5: Conclusion

The WebBrowser control is a good control for enabling web content in Windows applications. The additions in this article can be used to overcome the obstacles that developers face when they have no control over what web pages or other content the user might visit with their application. Hopefully, the next version of the .NET Framework will give us a little extra.

The sample application and source

The sample application is not a flashy UI, but it does demonstrate everything about this article. The code is commented, and hopefully gives enough information for helping you put your own solution together.


I would like to thank the following persons that made part of this article possible:

  • The technique for DWebBrowserEvents2 was in the bug list of Microsoft .NET 2.0. This has been used with modification.
  • The technique for WndProc was told by someone called "JoeBlow" on the MSDN forums, who had it derived from the MSKB article #253219.

This is my first article on The Code Project. Please excuse me for my English. Thanks for reading! If you can add anything or have suggestions or tips, please post a message below.


This code is copyrighted by The Wheel Automatisering in The Netherlands. Some rights are reserved.

The code in this license may be used for any purpose, just let your users know where it came from, and share derived code under the same license as this one. Don't blame me if something goes wrong. More information can be found here.

If you wish to use and/or publish this in commercial closed-source applications, you have my consent. You may use this code under your own license when you do so.


  • 27th of March 2006: First post of this article.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Jeroen Landheer
Web Developer
Chile Chile
I am
- born in The Netherlands
- living in Chile together with my wife.
- a Microsoft Certified Professional Developer on all 3 areas (Windows, Web and Enterprise)
- an MCITP on Microsoft SQL Server 2005 (Database Administrator)
- an active programmer for about 14 years.
- a business owner, of a Dutch company called "The Wheel Automatisering" (
- a coder in C#, VB.Net and Managed C++.
- someone who likes to share knowledge

For fun I like to go out with my dogs, enjoy the sun or write some articles that I share with the community.

You may also be interested in...

Comments and Discussions

GeneralRe: VB.NET version of ExtendedWebBrowser Pin
patrickdupont2-Jul-09 4:51
memberpatrickdupont2-Jul-09 4:51 
GeneralRe: VB.NET version of ExtendedWebBrowser Pin
sujtha7-Jul-10 1:17
membersujtha7-Jul-10 1:17 
QuestionProblem in Rendering with in the web browser control. Pin
SabariGE22-Mar-09 23:39
memberSabariGE22-Mar-09 23:39 
AnswerRe: Problem in Rendering with in the web browser control. Pin
b-k-w27-Oct-10 10:51
memberb-k-w27-Oct-10 10:51 
Questionlicense? Pin
Member 417923011-Mar-09 21:44
memberMember 417923011-Mar-09 21:44 
AnswerRe: license? Pin
Jeroen Landheer12-Mar-09 4:48
memberJeroen Landheer12-Mar-09 4:48 
GeneralWeb browser control allowing single click on flash objects(buttons/links) Pin
Tapas Ranjan Singh21-Jan-09 2:04
memberTapas Ranjan Singh21-Jan-09 2:04 
GeneralRe: Web browser control allowing single click on flash objects(buttons/links) Pin
sarria88@gmail.com29-Apr-09 4:57
membersarria88@gmail.com29-Apr-09 4:57 
GeneralRe: Web browser control allowing single click on flash objects(buttons/links) Pin
Tapas Ranjan Singh30-Apr-09 3:06
memberTapas Ranjan Singh30-Apr-09 3:06 
GeneralRe: Web browser control allowing single click on flash objects(buttons/links) Pin
mkistler29-Jul-09 23:56
membermkistler29-Jul-09 23:56 
you rock!
Big Grin | :-D


--== Matthieu ==-

GeneralWeb Browser data interaction... Pin
Nagaraj Muthuchamy7-Jan-09 19:11
memberNagaraj Muthuchamy7-Jan-09 19:11 
QuestionHow do I call this browser from my C# code? Pin
JamesMMM9-Dec-08 4:48
memberJamesMMM9-Dec-08 4:48 
GeneralApply to my project Pin
rav3n_pl10-Sep-08 13:48
memberrav3n_pl10-Sep-08 13:48 
GeneralVery Interesting article Pin
Ralph Varjabedian28-Aug-08 23:44
memberRalph Varjabedian28-Aug-08 23:44 
GeneralApply CSS to DocumentText Pin
Jabes29-Jul-08 21:32
memberJabes29-Jul-08 21:32 
QuestionUnable to Browse the Application from Extended WebBrowser Pin
Arudra_PLS24-Jul-08 2:19
memberArudra_PLS24-Jul-08 2:19 
AnswerRe: Unable to Browse the Application from Extended WebBrowser Pin
Jeroen Landheer24-Jul-08 8:55
memberJeroen Landheer24-Jul-08 8:55 
GeneralExtended WebBrowser Control Tests Pin
DrTom219-Jul-08 15:51
memberDrTom219-Jul-08 15:51 
GeneralHi thanks for the great program. Pin
Zaphieon26-Jun-08 18:37
memberZaphieon26-Jun-08 18:37 
GeneralI want to use the web browser to open a webpage that has a swf file Pin
lungiez120-Jun-08 5:09
memberlungiez120-Jun-08 5:09 
QuestionHow to disable alert messages of java script Pin
asif_kalim19-Jun-08 3:29
memberasif_kalim19-Jun-08 3:29 
AnswerRe: How to disable alert messages of java script Pin
zhifu19-Jun-08 20:53
memberzhifu19-Jun-08 20:53 
GeneralRe: How to disable alert messages of java script Pin
asif_kalim20-Jun-08 1:43
memberasif_kalim20-Jun-08 1:43 
GeneralRe: How to disable alert messages of java script Pin
MenaceSan26-Sep-08 9:42
memberMenaceSan26-Sep-08 9:42 
QuestionHow can I use WindowClosing Event? [modified] Pin
Johnsoong30-May-08 22:13
memberJohnsoong30-May-08 22:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.1509028.1 | Last Updated 29 Mar 2006
Article Copyright 2006 by Jeroen Landheer
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid