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

How To Close The Form Hosting The Webbrowser Control When Scripting Calls Window Close In The Net Framework Version 2 0

- 25 May 2007

By design the WebBrowser Control does not close the parent Form when WebBrowser Control is closed.  Here is one way you can accomplish this.

The WebBrowser will send a WM_NOTIFYPARENT notification that can be captured and an event raised that a host can respond to.

Here is the documentation on the WM_NOTIFYPARENT notification:  http://msdn2.microsoft.com/en-us/library/ms632638.aspx

In this case we care about when the WebBrowser is closed, so we will respond to the wParam WM_DESTROY only.  Since I am going to override the WebBrowser control for other purposes, I will simply add an event to this control that any Form can act on.

First define a new VB Windows Project.  Drag and drop the WebBrowser Control onto your form from the Common Controls section of the Toolbox.

Next we want to extend the WebBrowser control so we can process the WndProc function.  Create a new Class(Add, Class) and call it MyExtendedBrowserControl.

<font color=#0000ff size=2>

Public

</font> <font color=#0000ff size=2>Class</font> MyExtendedBrowserControl <font color=#008000 size=2>‘ Based on WebBrowser
</font><font color=#0000ff size=2>Inherits</font> System.Windows.Forms.WebBrowser

</font><font color=#008000 size=2>‘ Define constants from winuser.h
</font><font color=#0000ff size=2>Private</font> <font color=#0000ff size=2>Const</font> WM_PARENTNOTIFY <font color=#0000ff size=2>As</font> <font color=#0000ff size=2>Integer</font> = &H210 <font color=#0000ff size=2>Private</font> <font color=#0000ff size=2>Const</font> WM_DESTROY <font color=#0000ff size=2>As</font> <font color=#0000ff size=2>Integer</font> = 2

</font><font color=#008000 size=2>‘Define New event to fire
</font><font color=#0000ff size=2>Public</font> <font color=#0000ff size=2>Event</font> WBWantsToClose()

</font><font color=#0000ff size=2>Protected</font> <font color=#0000ff size=2>Overrides</font> <font color=#0000ff size=2>Sub</font> WndProc(<font color=#0000ff size=2>ByRef</font> m <font color=#0000ff size=2>As</font> Message) <font color=#0000ff size=2>    Select</font> <font color=#0000ff size=2>Case</font> m.Msg <font color=#0000ff size=2>        Case</font> WM_PARENTNOTIFY <font color=#0000ff size=2>            If</font> (<font color=#0000ff size=2>Not</font> DesignMode) <font color=#0000ff size=2>Then
</font><font color=#0000ff size=2>                If</font> (m.WParam = WM_DESTROY) <font color=#0000ff size=2>Then
</font><font color=#008000 size=2>                    ‘ Tell whoever cares we are closing
</font><font color=#0000ff size=2>                    RaiseEvent</font> WBWantsToClose() <font color=#0000ff size=2>                End</font> <font color=#0000ff size=2>If
</font><font color=#0000ff size=2>            End</font> <font color=#0000ff size=2>If
</font>            DefWndProc(m) <font color=#0000ff size=2>        Case</font> <font color=#0000ff size=2>Else
</font><font color=#0000ff size=2>            MyBase</font>.WndProc(m) <font color=#0000ff size=2>    End</font> <font color=#0000ff size=2>Select
</font><font color=#0000ff size=2>End</font> <font color=#0000ff size=2>Sub

End

</font> <font color=#0000ff size=2>Class</font>

Now we have to get into the guts of the Form.  Although we dropped a WebBrowserControl on the form (and VB wired that up nicely for us) we REALLY want to create our new Control that extends the WebBrowser Control.

Choose File, Open from the Visual Studio menu and open the designer vb code (in this case Form1.Designer.vb).

Replace System.Windows.Forms.WebBrowser with the extended control MyExtendedBrowserControl.  There should be Two Places:

<font color=#0000ff size=2>Me</font>.WebBrowser1 = <font color=#0000ff size=2>New</font> VBCloseBrowser.ExtendedWebBrowser.ExtendWebBrowser

</font><font color=#0000ff size=2>Friend</font> <font color=#0000ff size=2>WithEvents</font> WebBrowser1 <font color=#0000ff size=2>As</font> VBCloseBrowser.ExtendedWebBrowser.ExtendWebBrowser

</font>Save all the files and Build the project (you will see errors in the designers until you do build).

Now go to the design view of your Form.  Right Click on the Browser control and choose Properties…  Click on the Events Icon (the lightning bolt) and you will see at the bottom your new event WBWantsToClose.  Double click on that name and you will automatically add the handler for that event.  In my case, I simply want to close this form so I added Me.Close() in the handler.

Enjoy!

<< Go Back