3.8. Put the Web in a Window.NET 2.0 provides a WebBrowser control that you can drag and drop directly into a Windows application. This gives you a full-featured Internet Explorer 6 browser embedded in your application, with virtually no coding required. 3.8.1. How do I do that?Create a new Windows application (WebBrowsing) and enlarge the form to hold a reasonably sized browser and a couple of other controls. Drag a WebBrowser control onto your form. You'll notice that it fills the form, but if you use its smart tag to click Undock in the parent container, you'll find you have a resizable WebBrowser control within the form. Click the URL property of the WebBrowser control and type the URL of your favorite web site (for example, http://www.LibertyAssociates.com). Run the application and voila! You're browsing my site. Let's add a few useful controls: a text box (with auto-complete for URLs), a progress bar, and Back and Forward buttons. The browser control fires a number of useful events as the page is loading. Some of the most useful are shown in Table 3-6.
Double-click each button on your form to set up their event handlers. Click the browser itself, and set a handler for the ProgressChanged event. Finally, set the Leave event for the browser, and set two events for the URL text box. The complete source code listing is shown in Example 3-2. Example 3-2. Integrating the WebBrowser control into your formusing System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace WebBrowsing { public partial class Form1 : Form { public Form1( ) { InitializeComponent( ); } private void webBrowser1_ProgressChanged( object sender, WebBrowserProgressChangedEventArgs e) { progressBar1.Value = (int)(e.CurrentProgress / e.MaximumProgress); } private void btnBack_Click(object sender, EventArgs e) { webBrowser1.GoBack( ); } private void btnForward_Click(object sender, EventArgs e) { webBrowser1.GoForward( ); } private void btnStop_Click(object sender, EventArgs e) { webBrowser1.Stop( ); } private void txtURL_Leave(object sender, EventArgs e) { if (txtURL.Text.Length < 1) { txtURL.Text = "http://www.LibertyAssociates.com"; } webBrowser1.Url = txtURL.Text; } private void webBrowser1_DocumentCompleted( object sender, WebBrowserDocumentCompletedEventArgs e) { txtURL.Text = webBrowser1.Url.ToString( ); } } } The only tricky code is for computing the value for the progress bar (which must be cast to an int). The result is shown in Figure 3-26. Figure 3-26. Browsing the Web3.8.2. What about......using the WebBrowser control to launch a standalone browser? You can do this; instead of setting the URL property just use the overloaded Navigate method: public void Navigate( string url, bool newWindow // set true for new window ); ...what about using the WebBrowser control to look at XML files with full collapse/expand support? Sure; just navigate to the XML file and the WebBrowser control will do the work for you, as illustrated in Figure 3-27. Figure 3-27. Examining an XML file3.8.3. Where can I learn more?The .NET Framework Windows Forms page (http://www.windowsforms.net/) covers this feature, as does the "WebBrowser Control" article in the MSDN. |