views:

34

answers:

2

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?

+1  A: 

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.

Brian Driscoll
A: 

WebBrowser works asynchronously so you have to subscribe to WebBrowser.Navigated and wait until it will navigate to URL given and render resulting HTML

abatishchev