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="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
<script language="javascript" type="text/javascript">
// <!CDATA[

function Button1_onclick() {
window.close();
}

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

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.

Workaround: 

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 =

true;

}

 

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 )

            {

                this.SuspendLayout();

                this.Controls.Remove(webBrowser1);

                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);

 

 

                this.Controls.Add(webBrowser1);

                this.ResumeLayout(false);

                webBrowser1.Visible = true;

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

            }

 

 

Let me know if this helps you out!