5

I have been searching for many hours but cannot find a working solution.

My c# webbrowser control (part of a winforms app) freezes when a js function is called to close the window.

Can I capture a js window.close event?

The closest solution I have found is the following but it's just not working (the compiler throws no errors):

HtmlDocument htmlDocument = this.webBrowser1.Document;
htmlDocument.Window.Unload += new HtmlElementEventHandler(Window_Unload);

And the method:

    void Window_Unload(object sender, HtmlElementEventArgs e)
    {
      MessageBox.Show("Display me when webbrowser control is disposed");
    }

This is only my second c# program and the first time I have been unable to find a working solution by myself.

noseratio
  • 59,932
  • 34
  • 208
  • 486
  • I think the use of the word 'disposed' makes the question confusing, because you're not asking about .NET disposal, but rather about some kind of self-destruction on the part of the webbrowser control (which is a wrapper on a big lump of unmanaged stuff) – Will Dean Nov 25 '13 at 23:52
  • I agree, I have revised the question to make it less ambiguous. I was assuming, perhaps falsely that the web browser control was being disposed of. –  Nov 26 '13 at 00:11

3 Answers3

0

In the original WebBrowser ActiveX control, which is wrapped by WinFroms WebBrowser control, there's WindowClosing event for this. Unfortunately, it doesn't get fired for the WinForms wrapper, for some reasons beyond our control.

One possible workaround is described here. Another workaround relies upon the fact that DOM window.onunload gets always fired when window.close() is called. You should work out the corresponding WebBrowser state transition. Transitions are different between navigation, windows.close and refresh/F5. More info here.

Community
  • 1
  • 1
noseratio
  • 59,932
  • 34
  • 208
  • 486
  • 1
    If anyone else gets stuck this link http://blogs.artinsoft.net/Mrojas/archive/2009/05/21/Extended-WebBrowser-Control-Series-WebBrowser-Control-and-windowClose().aspx provides a demo for an extended webbrowser class that handles window.close events. –  Dec 01 '13 at 17:14
0

The WebBrowser control doesn't come with a built in method to detect JavaScript.Close event. You can inject your own JS to help you handle this OR you can extend the WebBrowser control to create an event when window.close() is fired and your WebBroser control is destroyed.

From this page, I got the following information http://blogs.msdn.com/b/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

This example uses VB but you can easily convert it to C#.

Create a new class file and put the following code in

Public Class MyExtendedBrowserControl
' Based on WebBrowser
Inherits System.Windows.Forms.WebBrowser

' Define constants from winuser.h
Private Const WM_PARENTNOTIFY As Integer = &H210
Private Const WM_DESTROY As Integer = 2

'Define New event to fire
Public Event WBWantsToClose()

Protected Overrides Sub WndProc(ByRef m As Message)
    Select Case m.Msg
        Case WM_PARENTNOTIFY
            If (Not DesignMode) Then
                If (m.WParam = WM_DESTROY) Then
                    ' Tell whoever cares we are closing
                    RaiseEvent WBWantsToClose()
                End If
            End If
            DefWndProc(m)
        Case Else
            MyBase.WndProc(m)
    End Select
End Sub

End Class

Now, you have to replace the built in WebControl evocation with this new one. Edit the form1.designer.vb file or whatever your form is called. To do this, click "Show All Files" icon in solution explorer to see the designer file. Once you have the designer file opened, replace System.Windows.Forms.WebBrowser with MyExtendedBrowserControl.

Build the project. You'll need to do this for the control to be compiled and have it show up without errors on the form itself.

Now that you built the project, you'll be able to edit the control and access the new WBWantsToClose event. Open the form, click the WebBrowser control, open its properties (F4), click the "events" icon (lightening bolt), double click "WBWantsToClose". That'll take you into the code with the event ready to be handled.

Michael Khalili
  • 933
  • 12
  • 13
-1

It really depends on how you are disposing it, you can use the Component.Disposed or Control.HandleDestroyed event which is what I would recommend if you are trying to dispose of the control.

Tweety
  • 192
  • 1
  • 10