why can't I navigate to a page in the webbrowser control during runtime?
When I do something like this
webBrowser1 = new WebBrowser();
webBrowser1.Url = new Uri("http://google.com");
webBrowser1.Navigate("http://google.com");
all I get is a blank window. when I step through this webBrowser1.Url stays 开发者_如何学运维= null after the second statement has executed. Why is that?
if I set the url property before I compile the web site loads correctly when I open the form. So why can't I load a site dynamically?
If you've added the Web Browser control at design-time, you don't need to instantiate it in code (InitializeComponent will take care of that for you).
Remove this line:
webBrowser1 = new WebBrowser();
...and it should work fine for you.
If you are declaring the control in code, then you must add it to the visual tree of the parent form:
this.Controls.Add(webBrowser1);
where "this" refers to your form.
I had it in the constructor and it worked when I moved it out. I now call a function after the form loads to set the control
WebBrowser
works asynchronously so you have to subscribe to WebBrowser.Navigated
and wait until it will navigate to URL given and render resulting HTML
精彩评论