Reputation: 89
I am using
System.Windows.Forms.WebBrowser
control in C# for displaying some pages. I want to do some custom work when the user clicks on a url of a page which does not exist.
Basically I want to set some values when the browser displays the following message
The page cannot be displayed
The page you are looking for is currently unavailable. The Web site might be experiencing technical difficulties
How can I get the status so that I can differenciate between a page loaded and error page?
Upvotes: 4
Views: 2263
Reputation: 1996
If you cast WebBrowser to the underlying ActiveX implementation, you can access the NavigateError event.
Note: You'll need to add a reference to SHDocVw. Confusingly, this is in the COM tab with the name "Microsoft Internet Controls" with a path of 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
}
Upvotes: 2
Reputation: 27339
You can use the CreateSink
method on the WebBrowser
control to access the NavigateError
event of the underlying WebBrowser ActiveX control. The System.Windows.Forms.WebBrowser
control is a managed wrapper for the WebBrowser ActiveX control, but it does not wrap all of the functionality of that ActiveX control. The NavigateError
event is available on the unmanaged ActiveX web browser control. CreateSink
will allow you to extend the functionality of the System.Windows.Forms.WebBrowser
control so you can handle the NavigateError
event.
From the documentation:
This method is useful if you are familiar with OLE development using the unmanaged WebBrowser ActiveX control and you want to extend the functionality of the Windows Forms WebBrowser control, which is a managed wrapper for the ActiveX control. You can use this extensibility to implement events from the ActiveX control that are not provided by the wrapper control.
MSDN has a full example here:
http://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.createsink.aspx
Upvotes: 1