Jeff Sanders Technical Blog

I am a Microsoft employee that has worked on all aspects of the Web Stack for a long time. I hope these blogs are useful to you! Use this information at your own risk.


<< Go Back

Window Close Freezes Net 2 0 Webbrowser Control In Windows Form Application

- 23 Apr 2008

In the spirit of my post: <a class=”” title=”How To: Close the Form hosting the WebBrowser control when scripting calls window.close in the .Net Framework version 2.0” href=”http://blogs.msdn.com/jpsanders/archive/2007/05/25/how-to-close-the-form-hosting-the-webbrowser-control-when-scripting-calls-window-close-in-the-net-framework-version-2-0.aspx” target=_blank mce_href=”http://blogs.msdn.com/jpsanders/archive/2007/05/25/how-to-close-the-form-hosting-the-webbrowser-control-when-scripting-calls-window-close-in-the-net-framework-version-2-0.aspx”>How To: Close the Form hosting the WebBrowser control when scripting calls window.close in the .Net Framework version 2.0</a> 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”>
    Untitled Page
<script language=”javascript” type=”text/javascript”>
// <!CDATA[

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

// ]]>
</script>
</head>

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

</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:<p mce_keep="true">

    Private Sub AxWebBrowser1_WindowClosing(ByVal sender As System.Object, ByVal e As AxSHDocVw.DWebBrowserEvents2_WindowClosingEvent) Handles AxWebBrowser1.WindowClosing<?xml:namespace prefix = o ns = “urn:schemas-microsoft-com:office:office” /></p> <p class=MsoNormal style="MARGIN: 0in 0in 0pt">

        e.cancel = True ‘ or do nothing and the app will close</p> <p class=MsoNormal style="MARGIN: 0in 0in 0pt">    End Sub</p> <p class=MsoNormal style="MARGIN: 0in 0in 0pt"> </p>

</p>

C# sample:

</font></o:p></span> </p>

</font><font color=#0000ff size=2><font color=#0000ff size=2>private</font></font> <font color=#0000ff size=2><font color=#0000ff size=2>void</font></font> axWebBrowser1_WindowClosing(<font color=#0000ff size=2><font color=#0000ff size=2>object</font></font> sender, AxSHDocVw.<font color=#2b91af size=2><font color=#2b91af size=2>DWebBrowserEvents2_WindowClosingEvent</font></font> e) </p>

{

     e.cancel =

</font><font color=#0000ff size=2><font color=#0000ff size=2>true</font></font>; </p>

}

 

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!

 

</font>

<< Go Back