我可以同时使用.NET WebBrowser控件发现错误?控件、错误、发现、NET

2023-09-03 11:08:21 作者:長情詴辠

我有一个净栌应用程序,通过web浏览器控件中显示的网页。

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.

推荐答案

web浏览器的Windows窗体控件大约是Internet Explorer的包装,并没有露出下面的ActiveX控件,特别是NavigateError事件的所有功能。这里有一个解决方法:

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:

首先添加引用SHDOCVW.DLL到项目(COM添加引用窗口选项卡)。然后,你可以做以下捕获错误:

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            
}