1

I have to code a program for a library, I have to embed a Web Browser for a website but the Web Browser can’t be close. So I have cancel closing but when a user clicks on a button in JS for closing window (window.Close) the program crash. I add :

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

when the Document is charged. This code works and gets to catch the evenment but I don’t know what I have to do in my function :

void Window_Unload(object sender, HtmlElementEventArgs e)
{

}

Can anyone help me please?

deltonio2
  • 1,719
  • 1
  • 23
  • 37
  • You are supposed to close your window when you get that notification, use this.Close(). That's surely not what you *really* want to do when you use words like "can't be close". Instead of guessing at a solution, document your *real* problem, no reason that your program should crash when javascript does something. Post the exception message and the stack trace. If the web page should not be allowed to navigate anywhere by itself then simply set the AllowNavigation property to False. If a button should be disabled or removed then do so in the DocumentCompleted event. – Hans Passant Mar 20 '14 at 17:41
  • @HansPassant Thank you for your answer! But The user should not close the webBrowser (the computer is here just for navigate in a webSite). And in the webSite, there is a JS button that close the window, and when I click it, there is a confirmation in a MessageBox. And if I click No (I don't whant to exit) there are no problems but if I click "Yes", the program don't respond. For the “AllowNavigation”, it’s ok, because there are no external links and the user can't enter an URL. And I don’t understand, can you remove a button in an html page? If yes, how do you do that? – deltonio2 Mar 21 '14 at 07:19

1 Answers1

1

You should handle the WindowClosing event on the underlying WebBrowser ActiveX Control (WebBrowser.ActiveXInstance). There is an option to cancel it (disclaimer: untested). Check this answer for more details on how to handle the "raw" WebBrowser events like this.

Community
  • 1
  • 1
noseratio
  • 59,932
  • 34
  • 208
  • 486