How do I know if an error page has loaded or a valid page in System.Windows.Forms.WebBrowser?

I use

System.Windows.Forms.WebBrowser 

in C # to display some pages. I want to do some custom work when a user clicks on the URL of a page that does not exist.

Basically, I want to set some values ​​when the following message is displayed in the browser

 The page cannot be displayed The page you are looking for is currently unavailable. The Web site might be experiencing technical difficulties 

How do I get status so that I can distinguish between page and error page?

+4
source share
2 answers

If you dropped WebBrowser into the base ActiveX implementation, you can access the NavigateError event.

Note. You need to add a link to SHDocVw. Vaguely, this is on the COM tab with the name "Microsoft Internet Controls" indicating the path c: \ windows \ system32 \ ieframe.dll

  private void button1_Click(object sender, EventArgs e) { //Note: you need to wait until the ActiveXInstance property is initialised. var axWebBrowser = (SHDocVw.WebBrowser)webBrowser1.ActiveXInstance; axWebBrowser.NavigateError += axWebBrowser_NavigateError; webBrowser1.Url = new Uri("http://www.thisisnotavaliddomain.com"); } void axWebBrowser_NavigateError(object pDisp, ref object URL, ref object Frame, ref object StatusCode, ref bool Cancel) { //handle your error } 
+2
source

You can use the CreateSink method in the WebBrowser to access the NavigateError event of the underlying WebBrowser ActiveX control. The System.Windows.Forms.WebBrowser control is a managed shell for the ActiveX WebBrowser control, but it does not transfer all the functionality of this ActiveX control. The NavigateError event is available on an unmanaged ActiveX web browser control. CreateSink allows you to extend the functionality of the System.Windows.Forms.WebBrowser control so that you can handle the NavigateError event.

From the documentation:

This method is useful if you are familiar with OLE development using an unmanaged ActiveX WebBrowser control, and you want to extend the functionality of the Windows Forms WebBrowser control, which is a managed shell for an ActiveX control. You can use this extensibility to implement events from an ActiveX control that are not provided by wrapper control.

MSDN has a complete example:
http://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.createsink.aspx

+1
source

Source: https://habr.com/ru/post/1392688/


All Articles