23.9. WebBrowser ClassWith FCL 2.0, Microsoft introduced the WebBrowser class (namespace System.Windows.Forms), which enables applications to incorporate Web browsing capabilities. The control provides methods for navigating Web pages and maintains its own history of Web sites visited. It also generates events as the user interacts with the content displayed in the control so that your application can respond to events such as the user clicking the links displayed in the content. Figure 23.8 demonstrates the WebBrowser class's capabilities. Class FrmBrowser provides the basic functionality of a Web browser, allowing the user to navigate to a URL, move backward and forward through the history of visited sites, and reload the current Web page. Figure 23.8. WebBrowser class example.
Lines 638 define five Click event handlers, one for each of the five navigation Buttons that appear at the top of the Form. Each event handler calls a corresponding Web-Browser method. WebBrowser method GoBack (line 9) navigates back to the previous page in the navigation history. Method GoForward (line 16) navigates forward to the next page in the navigation history. Method Stop (line 23) stops loading the current page. Method Refresh (line 30) reloads the current page. Method GoHome (line 37) navigates to the user's home page, as defined under Internet Explorer's settings (under Tools > Internet Options... in the Home page section). The TextBox to the right of the navigation buttons allows the user to enter the URL of a Web site to browse. When the user types each keystroke in the TextBox, the event handler in lines 4148 executes. If the key pressed was Enter, line 46 calls WebBrowser method Navigate to retrieve the document at the specified URL. A WebBrowser object generates a Navigating event when it starts loading a new page. When this occurs, the event handler in lines 5156 executes, and line 55 enables btnStop so that the user can cancel the loading of the Web page. Typically, a user can see the status of a loading Web page at the bottom of the browser window. For this reason, we include a TextBox control (named txtStatus) and a ProgressBar control (named prgPage) at the bottom of our Form. A WebBrowser object generates a StatusTextChanged event when its StatusText property changes. The event handler for this event (lines 5964) assigns the new contents of the WebBrowser's Status Text property to txtStatus's Text property (line 63) so that the user can monitor the WebBrowser's status messages. A WebBrowser object generates a ProgressChanged event when its page-loading progress is updated. The ProgressChanged event handler (lines 6779) updates prgPage's Value (lines 7677) to reflect how much of the current document has been loaded. When the WebBrowser finishes loading a document, it generates a DocumentCompleted event. This executes the event handler in lines 8298. Line 87 updates the contents of txtNavigation so that it shows the URL of the currently loaded page (WebBrowser property Url). This is particularly important if the user browses to another Web page by clicking a link in the existing page. Lines 9091 use properties CanGoBack and CanGoForward to determine whether the back and forward buttons should be enabled or disabled. Since the document is now loaded, line 94 disables btnStop. Line 97 sets prgPage's Value to 0 to indicate that no content is currently being loaded. Lines 101106 define an event handler for the DocumentTitleChanged event, which occurs when a new document is loaded in the WebBrowser. Line 105 sets FrmBrowser's Text property (which is displayed in the Form's title bar) to the WebBrowser's current DocumentTitle. |