AppBar, WebBrowser Control and Internet Explorer URL gives InvalidCastException
Posted on 2007-10-17
I have created an AppBar in c# that docks to the LEFT of the desktop. The AppBar has a WebBrowser control in it. The AppBar then attempts to get the currently active Internet Explorer and capture the BeforeNavigate2 event so that I can later use the changed URL.
foreach (SHDocVw.InternetExplorer ie in shellWindows)
ie.BeforeNavigate2 += ie_BeforeNavigate2;
This does capture the event, however when I try to call a function in the webbrowser control after this event with the following code, I get an InvalidCastException:
public void ie_BeforeNavigate2(object pDisp, ref object url, ref object Flags, ref object TargetFrameName, ref object PostData, ref object Headers, ref bool Cancel)
System.Console.Out.WriteLine("Process URL: " + url);
object ret = this.webBrowser1.Document.InvokeScript("callFunc");
catch (Exception e)
It appears that any reference to webBrowser1.document can't be made. I am unclear how to resolve this.