
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
806
|
Chapter 14: Web
When you add your HTML (<h1>Hey you added some HTML!</h1>), it is displayed as
shown in Figure 14-2.
The code to accomplish this is rather simple:
this._webBrowser.Document.Body.InnerHtml = "<h1>Hey you added some HTML!</h1>";
The navigation to a web page is equally trivial:
Uri uri = new Uri(this._txtAddress.Text);
this._webBrowser.Navigate(uri);
The nice thing about the navigation is the Navigated event that can be subscribed so
you are notified when the navigation has completed. This allows code to spin this off
in a thread and then come back to it once it is fully loaded. The event provides a
WebBrowserNavigatedEventArgs class that has a Url property to tell the URL of the
document that has been navigated to.
private void _webBrowser_Navigated(object sender, WebBrowserNavigatedEventArgs e)
{
// Update with where we ended up in case of redirection
// from the original URI.
this._txtAddress.Text = e.Url.ToString( );
// Set up the buttons if we can go back or forth.
this._btnBack.Enabled = this._webBrowser.CanGoBack;
this._btnForward.Enabled = this._webBrowser.CanGoForward;
}
Discussion
In the 1.x versions of the .NET Framework, embedding a web browser in your Win-
Forms application was much more difficult and error-prone. With the advent of the
2.0 Framework, there ...