Recipe 17.1. Displaying Web Pages on a Form


Problem

You want to display a web page on your form, possibly built from custom HTML content.

Solution

Sample code folder: Chapter 17\CustomWebContent

Sample code folder: Chapter 17\ WebBrowser

Use the WebBrowser control. It encapsulates the core Microsoft Internet Explorer engine, and it integrates easily into your Windows Forms applications.

Discussion

This recipe's sample code implements a simple web browser. Create a new Windows Forms application, and add the following controls to Form1:

  • A Panel control named WebToolbar. Set its Dock property to Top and its Size.Height property to about 40.

  • A WebBrowser control named WebContent. Set its Dock property to Fill. It should only fill below the Panel control. If it doesn't, right-click on the Panel control and select "Send to Back" from the shortcut menu.

  • A Button control named ActBack. This control should appear on the surface of the Panel control. Set its Text property to &Back.

  • A Button control named ActHome. This control should appear on the surface of the Panel control. Set its Text property to &Home.

  • A TextBox control named WebAddress. This control should appear on the surface of the Panel control. Set its Anchor property to Top, Left, Right.

  • A Button control named ActGo. This control should appear on the surface of the Panel control. Set its Text property to &Go and its Anchor property to Top, Right.

Arrange the controls as presented in Figure 17-1.

Figure 17-1. Controls for the web browser sample


Now add the following source code to the form's class template:

 Private Sub ActBack_Click(ByVal sender As System.Object, _       ByVal e As System.EventArgs) Handles ActBack.Click    ' ----- Move to the previous web page.    If (WebContent.CanGoBack() = True) Then _       WebContent.GoBack() End Sub Private Sub ActHome_Click(ByVal sender As Object, _       ByVal e As System.EventArgs) Handles ActHome.Click    ' ----- Move to the main web page.    WebContent.GoHome() End Sub Private Sub Form1_Load(ByVal sender As Object, _       ByVal e As System.EventArgs) Handles Me.Load    ' ----- Start from the home page.    ActHome.PerformClick() End Sub Private Sub ActGo_Click(ByVal sender As Object, _       ByVal e As System.EventArgs) Handles ActGo.Click    ' ----- Move to the requested page.    If (Trim(  WebAddress.Text) <> "") Then _         WebContent.Navigate(WebAddress.Text) End Sub 

The previous dozen lines of code are all you need to provide your users with a full Internet browsing experience (albeit without all of the fancy features). Run the program, and use it like a typical web browser.

You are not limited to Internet-based HTML content in the browser. You can supply your own generated-on-the-fly content as well, by setting the control's DocumentText property to a string containing the HTML content. We added the following code to a new Form1 that contained only a WebBrowser control:

 WebBrowser1.DocumentText = "<html><body>" & _    "<h1>Important</h1><p>This is web content." & _    "</p></body></html>" 

Figure 17-2 shows the output.

Figure 17-2. Custom HTML content in a WebBrowser control


Interacting with web-browser links is somewhat indirect. There is no LinkClicked event that occurs when a user clicks on a link. However, there is a Navigating event that is pretty close. You can monitor this event to provide support for your own internal link events. Decorate your custom HTML with a fake URL address, such as internal://EditCustomer?ID=25 to trigger the editing of the customer with ID number 25. To test this, create a new Windows Forms application, and add a WebBrowser control named WebBrowser1. Next, add the following source code to the form's code template:

 Private Sub Form1_Load(ByVal sender As Object, _       ByVal e As System.EventArgs) Handles Me.Load    ' ----- Add some custom content.    WebBrowser1.DocumentText = "<html><body>" & _       "<h1>Select an Airport</h1>" & _       "<p><a href=""internal://Airport?Code=LAX"">" & _       "Los Angeles</a></p>" & _       "<p><a href=""internal://Airport?Code=JFK"">" & _       "New York</a></p>" & _       "<p><a href=""internal://Airport?Code=SEA"">" & _       "Seattle</a></p>" & _       "</body></html>" End Sub Private Sub   WebBrowser1_Navigating(ByVal sender As Object, _       ByVal e As System.Windows.Forms. _         WebBrowserNavigatingEventArgs) _       Handles WebBrowser1.Navigating    ' ----- Which link was clicked?    Dim queryEntries() As String    Dim oneEntry() As String    Dim airportCode As String = "Invalid Code"    Dim scanQuery As String    ' ----- Look for internal://airport?… links.    If (e.Url.Scheme = "internal") Then       If (e.Url.Host = "airport") Then          If (e.Url.Query.Length > 0) Then             ' ----- Found an airport link. Get the             '       airport code. The query starts with             '       "?". Skip it.             queryEntries = _                Split(e.Url.Query.Substring(1), "&")             For Each scanQuery In queryEntries                oneEntry = Split(scanQuery, "=")                If (UCase(oneEntry(0)) = "CODE") Then                   ' ----- Found the airport code.                   airportCode = UCase(oneEntry(1))                   Exit For                End If             Next scanQuery          End If          ' ----- Show the code.          MsgBox(airportCode)          e.Cancel = True       End If    End If End Sub 

Clicking on one of the links gives results similar to Figure 17-3.

Several of the WebBrowser control's properties can be used to limit the allowed actions of the user. For instance, setting the AllowNavigation, WebBrowserShortcutsEnabled, and IsWebBrowserContextMenuEnabled properties to False can effectively shut down all user interaction with the Internet, providing a portal for static web content display only.

Figure 17-3. An internally processed link


See Also

Recipe 17.12 shows how to add a clickable hyperlink to a Windows form.




Visual Basic 2005 Cookbook(c) Solutions for VB 2005 Programmers
Visual Basic 2005 Cookbook: Solutions for VB 2005 Programmers (Cookbooks (OReilly))
ISBN: 0596101775
EAN: 2147483647
Year: 2006
Pages: 400

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net