I have code such as this:

string url = "http://en.wikipedia.org/wiki/Earth";
System.Windows.Forms.WebBrowser wb = new System.Windows.Forms.WebBrowser();
wb.Url = new Uri(url);

but after that last line wb.Url is always null.

Also, if the last line is replaced with:


wb.Url and wb.Document are always null. What is going on here? This is the same exact code as in documentation, but it's not working.

I also tried to use SHdocVw:

object o = null;
InternetExplorer ie = new InternetExplorer();
IWebBrowser2 wb1 = (IWebBrowser2)ie;
wb1.Navigate(url, ref o, ref o, ref o, ref o);
HtmlDocument htmlDoc = (HtmlDocument) wb1.Document;
HtmlElementCollection children = htmlDoc.All;

but the wb1.Document returns an E_FAIL HRESULT.

Any help would be appreciated--I'm brand new to C#.


ddanbe commented: well posed question +6

>but after that last line wb.Url is always null.

That's true. It will remains null till document is not completely rendered.

private void button1_Click(object sender, EventArgs e)
            webBrowser1.Url = new Uri("http://en.wikipedia.org/wiki/Earth");
            webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);

        void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)

commented: Good, as always :) +6