window.close() freezes .NET 2.0 WebBrowser Control in Windows Form application

In the spirit of my post: How To: Close the Form hosting the WebBrowser control when scripting calls window.close in the .Net Framework version 2.0 the .NET 2.0 WebBrowser control has no control over the close events like WindowClosing.  One valid solution is to use the ActiveX version of the WebBrowser control.  Currently the .NET 2.0 WebBrowser contol simply wraps the ActiveX version anyhow!

Symptoms:  Create a .NET 2.0 Windows application and drop the Manged WebBrowser control from the toolbox on the form.  Navigate to a page that has jscript to close the window, for example:

<html xmlns="" >
<head runat="server">
    <title>Untitled Page</title>
<script language="javascript" type="text/javascript">
// <!CDATA[

function Button1_onclick() {

// ]]>
    <a href="javascript:window.close();">When you click me I close!</a>
    <input id="Button1" type="button" value="button" onclick="return Button1_onclick()" />

When you click the button or hyperlink, the control will seem to freeze.

Analysis:  This is a known issue.  What is happening is that the control is being released and coming down.  It looks like it is freezing.


Remove the WebBrowser from your form and add the UnManaged control to your toolbox.

To add the AxWebBrowser control make sure you are in design mode of a form.  Right click on the tool box and choose ‘Choose items…’.  Click on the COM Components tab and scroll down to find ‘Microsoft Web Browser’.  Check it and hit OK.  The WebBrowser will appear under the General Category of your toolbox items.

Then drop the browser you added onto the form and sink the WindowClosing event of the browser from the design view properties.  Setting e.cancel to true will keep the Close from happening.  If you do nothing (don’t set e.cancel) the app will close.

VB.NET sample:

    Private Sub AxWebBrowser1_WindowClosing(ByVal sender As System.Object, ByVal e As AxSHDocVw.DWebBrowserEvents2_WindowClosingEvent) Handles AxWebBrowser1.WindowClosing

        e.cancel = True ' or do nothing and the app will close

    End Sub


C# sample:

private void axWebBrowser1_WindowClosing(object sender, AxSHDocVw.DWebBrowserEvents2_WindowClosingEvent e)


     e.cancel =




Finally you could keep the Managed WebBrowser control and poll it's state with a timer dropped on the form.  If it is uninitialized you could use code similar to this to bring it back to life:

if (webBrowser1.ReadyState == WebBrowserReadyState.Uninitialized )


      //code here to deal with the uninitialized control!



Something like this would be your final code (copy the initialization code from the designer.cs file:


            if (webBrowser1.ReadyState == WebBrowserReadyState.Uninitialized )




                webBrowser1 = null;

                this.webBrowser1 = new System.Windows.Forms.WebBrowser();

                this.webBrowser1.Location = new System.Drawing.Point(109, -5);

                this.webBrowser1.MinimumSize = new System.Drawing.Size(20, 20);

                this.webBrowser1.Name = "webBrowser1";

                this.webBrowser1.Size = new System.Drawing.Size(250, 250);

                this.webBrowser1.TabIndex = 2;

                this.webBrowser1.Navigated += new System.Windows.Forms.WebBrowserNavigatedEventHandler(this.webBrowser1_Navigated);





                webBrowser1.Visible = true;

                webBrowser1.Url = new System.Uri("http://jsandershv2003/closme.htm");




Let me know if this helps you out!


Comments (1)

  1. Alex440 says:

    Great! Thank You so much!

    that's really the only way that worked for me!

    I've read so much posts about this stuff, and no one before your didn't really work!

    I've tried

    1. to get the activex control with SHDocVw,

    2. to override WndProc and catch WM_PARENTNOTIFY message with WM_DESTROY parameter,

    3. to subscribe to HtmlDocument.WindowUnload event,

    and nothing cathced the event! (only WndProc catched the message, but component nevertheless got destroyed)

    Only to replace the Windows.Forms.WebBrowser with AxSHDocVw.AxWebBrowser really solved the issue!

    Thank you so much!

Skip to main content