0

I use a Windows form application with a webBrowser to navigate on the internet. How can I close the application when a specific URL is load?

I use the following code to load the first web page:

webBrowser1.Navigate("www.google.com");

The following pages are loaded using the search engine.

Simone Gaggio
  • 73
  • 1
  • 1
  • 13
  • Does `Application.Exit();` after the Navigate work? I imagine if you want it to be based on a specific URL you'd just have some if tests around it. – Dortimer Mar 13 '19 at 19:58
  • ok but is there a function that is executed when a new web page is loaded? – Simone Gaggio Mar 13 '19 at 20:07
  • I think you could add an event handler for webBrowser's 'Navigated' event for custom logic https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.webbrowser.navigated?view=netframework-4.7.2 – Dortimer Mar 13 '19 at 20:11
  • Use the `DocumentCompletedEvent`. In the event handler, check the [WebBrowser.ReadyState](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.webbrowser.readystate). When it's `WebBrowserReadyState.Complete`, you're quite sure the Document has been loaded completely. The `DocumentCompletedEvent` can be raised multiple times. See the notes here: [How to get an HtmlElement value inside Frames/IFrames?](https://stackoverflow.com/a/53218064/7444103) about this. – Jimi Mar 13 '19 at 20:23

1 Answers1

0

You have to use the DocumentCompleted event. When that event is fired you know the page loaded, and then you can check the url and if it is the one you are looking for you do Application.Exit().

More info here: https://learn.microsoft.com/en-us/dotnet/framework/winforms/controls/webbrowser-control-overview

Hope this helps!

Matt
  • 319
  • 2
  • 7
  • I alredy try this but but it does not work. Can you show me an example code? – Simone Gaggio Mar 13 '19 at 20:17
  • Im sorry but I have a big chunk of code that has this working just by handling the event I mentioned like this: private void WebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) linked to the web browser. Can you show me what are you doing? – Matt Mar 13 '19 at 20:37