Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I detect errors while using a .Net WebBrowser control?

I have an .Net Froms application that displays web pages through a WebBrowser control.

Is there anyway that I can detect if the control shows a 'Page not found' or 'Cannot display webpage' error? There doesn't seem to be any error event handlers.

like image 628
tpower Avatar asked Dec 15 '08 14:12

tpower


People also ask

How do I stop a script error in WebBrowser control?

In this case, set ScriptErrorsSuppressed to false and suppress script errors in a handler for the HtmlWindow.

What is WebBrowser control?

The WebBrowser control provides a managed wrapper for the WebBrowser ActiveX control. The managed wrapper lets you display Web pages in your Windows Forms client applications.

What is use of Web browser control in VB net?

The Web Browser control in VB.NET allows you to host Web pages and other web browser enabled documents in your Windows Forms applications. You can add browser control in your VB.Net projects and it displays the web pages like normal commercial web browsers .


2 Answers

The WebBrowser windows forms control is wrapper around Internet Explorer and it doesn't expose all the functionality of the underlying ActiveX control and particularly the NavigateError event. Here's a workaround:

First add reference to SHDocVw.dll to your project (COM tab of Add Reference window). Then you can do the following to capture errors:

private void button1_Click(object sender, EventArgs e)
{
    SHDocVw.WebBrowser instance = (SHDocVw.WebBrowser)webBrowser1.ActiveXInstance;
    instance.NavigateError += new SHDocVw.DWebBrowserEvents2_NavigateErrorEventHandler(instance_NavigateError);
    webBrowser1.Navigate("http://www.google.com/foo");
}

void instance_NavigateError(object pDisp, ref object URL, ref object Frame, ref object StatusCode, ref bool Cancel)
{
    // Do whatever you want with the error            
}
like image 191
Darin Dimitrov Avatar answered Sep 27 '22 20:09

Darin Dimitrov


I found another way to solve this without setting a reference to the SHDocVw dll.

See web browser CreateSink method on MSDN.

like image 25
BenR Avatar answered Sep 27 '22 20:09

BenR