Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Catching and handling Web Browser Close Event

I am Using Windows Forms and a WebBrowser Control, I am in need of catching and handling the WebBrowser Controls call to close.

I have looked at this but it is not working as I need it to: http://blogs.artinsoft.net/Mrojas/archive/2009/05/21/Extended-WebBrowser-Control-Series-WebBrowser-Control-and-windowClose().aspx

This is the message I get:

---------------------------
Web Browser
---------------------------
The webpage you are viewing is trying to close the window.

Do you want to close this window?
---------------------------
Yes   No   
---------------------------

I need to catch this event and suppress it, then perform a function after that. Does anyone know a simple way to do this?

like image 616
Rusty Nail Avatar asked Dec 26 '22 09:12

Rusty Nail


1 Answers

After searching everywhere and not really getting what I needed, I managed to stumble onto a by chance reference:

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

The Document does get Unloaded before the WebBrowser Control is closed and disposed.

void Window_Unload(object sender, HtmlElementEventArgs e)
{
    // Do your stuff here...
    // Call Method...
    // Close Form...
}

And this is now working for me.

like image 96
Rusty Nail Avatar answered Dec 28 '22 23:12

Rusty Nail