none
Using WebBrowser control

    Question

  • 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.Navigate(url);

    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#.

    Thanks,

    Tyler

    • Moved by CoolDadTxMVP Monday, April 26, 2010 2:09 PM WinForms related (From:Visual C# General)
    Friday, April 23, 2010 7:28 PM

Answers

  • Hi tyler_kid_a;

    The Url property of the WebBrowser is responisble for getting and setting the url of the document which is loaded in the WebBrowser control.

    Try out this code sample (I created a WebBrowser control with a button)

    private void button1_Click(object sender, EventArgs e)

            {

                string url = "http://en.wikipedia.org/wiki/Earth"; 

                webBrowser1.Url = new Uri(url);

                webBrowser1.Navigate(url);

                webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);

                //MessageBox.Show(webBrowser1.Url.ToString());

            }

     

            private void webBrowser1_DocumentCompleted(object sender, EventArgs e)

            {

                MessageBox.Show(webBrowser1.Url.ToString());

            }

     

    When you run the above code normally the MessageBox will dispaly the correct Url of the document.

    Run it again after un-commenting the bolded line then you'll get a NullReference error.

    This shows when getting the value of the Url property of the WebBrowser control it gets the url value of the loaded document. That means the runtime

    reached the bolded the line before the document has finished loading.

     


    Regards Puboo My blog : http://www.thurupathan.spaces.live.com/
    • Proposed as answer by Helen Zhou Tuesday, April 27, 2010 6:53 AM
    • Marked as answer by Helen Zhou Friday, April 30, 2010 3:19 AM
    Monday, April 26, 2010 1:47 PM
  • Hi tyler_kid_a;

    You can also refer to this thread: http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/7c5a6d3c-b2b7-4575-b308-49f545217e39

    Sincerely,
    Helen Zhou


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    • Marked as answer by Helen Zhou Friday, April 30, 2010 3:27 AM
    Friday, April 30, 2010 3:27 AM

All replies

  • Hi tyler_kid_a;

    The Url property of the WebBrowser is responisble for getting and setting the url of the document which is loaded in the WebBrowser control.

    Try out this code sample (I created a WebBrowser control with a button)

    private void button1_Click(object sender, EventArgs e)

            {

                string url = "http://en.wikipedia.org/wiki/Earth"; 

                webBrowser1.Url = new Uri(url);

                webBrowser1.Navigate(url);

                webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);

                //MessageBox.Show(webBrowser1.Url.ToString());

            }

     

            private void webBrowser1_DocumentCompleted(object sender, EventArgs e)

            {

                MessageBox.Show(webBrowser1.Url.ToString());

            }

     

    When you run the above code normally the MessageBox will dispaly the correct Url of the document.

    Run it again after un-commenting the bolded line then you'll get a NullReference error.

    This shows when getting the value of the Url property of the WebBrowser control it gets the url value of the loaded document. That means the runtime

    reached the bolded the line before the document has finished loading.

     


    Regards Puboo My blog : http://www.thurupathan.spaces.live.com/
    • Proposed as answer by Helen Zhou Tuesday, April 27, 2010 6:53 AM
    • Marked as answer by Helen Zhou Friday, April 30, 2010 3:19 AM
    Monday, April 26, 2010 1:47 PM
  • Hi tyler_kid_a;

    You can also refer to this thread: http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/7c5a6d3c-b2b7-4575-b308-49f545217e39

    Sincerely,
    Helen Zhou


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    • Marked as answer by Helen Zhou Friday, April 30, 2010 3:27 AM
    Friday, April 30, 2010 3:27 AM