I'd like to create WebBrowser control programically in my code and then read page content.
I think I'm doing the same thing as designer does but control dynamically created doesn't work (DocumentText return empty string)
What I'm doing wrong ??
EDIT-2: Code change after @Axarydax suggestion (working)
Main block code:
WebBrowser browser = new WebBrowser { Name = "myBrowser"};
browser.DocumentCompleted += browser_DocumentCompleted;
browser.Navigate("www.google.com");
while (pageLoaded == false) {
Thread.Sleep(500); // pageLoaded is local field
Application.DoEvents(); // didn't wotk without this...
}
Console.WriteLine(browser.DocumentText);
Event Handler code
void browser_DocumentCompleted ( object sender, WebBrowserDocumentCompletedEventArgs e ) {
pageLoaded = true;
}