How to: Use the WebBrowser Control in the .NET Compact Framework

The .NET Compact Framework supports core functions for the Windows Forms WebBrowser control. The following members require Windows Mobile version 5.0 software for Pocket PC or Smartphone. This list is subject to change.

The following considerations apply only to Windows Mobile 2003 for Pocket PC and Windows Mobile 2003 for Smartphone:

On a device other than a Pocket PC or Smartphone that is running Microsoft Windows CE 5.0, the Navigated and DocumentCompleted events both occur twice when a new URL is visited. This error is planned to be fixed in future releases of Windows CE.

You can create a WebBrowser instance to respond to the HelpRequested event to display online Help topics for your application.

The .NET Compact Framework does not support the Document property and its related properties and events, except for the DocumentText property. You can use DocumentText to present HTML to your users, such as to provide links and a simple HTML form, but the .NET Compact Framework does not support accessing the HTML content of a Web page with this property.

You can determine the response to the form with the WebBrowserDocumentCompletedEventArgs.Url property in code that handles the Navigating event. A code example is provided in the first procedure that follows.

You cannot navigate out of a WebBrowser control in a Smartphone application. As a workaround, you can detect a key event and set the focus on another control. A code example is provided in the second procedure that follows.

Note

This workaround requires Windows Mobile 5.0 or the .NET Compact Framework 3.5.

For general information about using the WebBrowser control, see How to: Add Web Browser Capabilities to a Windows Forms Application.

To collect information from embedded HTML controls

  1. Use the DocumentText property to display HTML in the WebBrowser control. This HTML contains a form with a link and a text box to specify a URL.

    Dim sb As New StringBuilder()
    sb.Append("<html><body>")
    sb.Append("<a href=")
    sb.Append("""")
    sb.Append("https://www.microsoft.com")
    sb.Append("""")
    sb.Append(">Microsoft</a><p>")
    sb.Append("Specify a URL:<br>")
    sb.Append("<form action=''><input type='text' name='address'/>")
    sb.Append("<br><input type='submit'>")
    sb.Append("</form></body></html>")
    webBrowser1.DocumentText = sb.ToString()
    
    StringBuilder sb = new StringBuilder();
    sb.Append("<html><body>");
    sb.Append("<a href=");
    sb.Append("\"");
    sb.Append("https://www.microsoft.com");
    sb.Append("\"");
    sb.Append(">Microsoft</a><p>");
    sb.Append("Specify a URL:<br>");
    sb.Append("<form action=''><input type='text' name='address'/>");
    sb.Append("<br><input type='submit'>");
    sb.Append("</form></body></html>");
    webBrowser1.DocumentText = sb.ToString();
    
  2. Use the Navigating event to determine whether the URL contains a response from the form. If it does, navigate to the URL.

    Private Sub webBrowser1_Navigating(ByVal sender As Object, ByVal e As WebBrowserNavigatingEventArgs)  Handles webBrowser1.Navigating
        Dim x As Integer 
        '  The URL contains the results of the 
        '  HTML form following the equals sign.
        x = e.Url.ToString().LastIndexOf("=")
        If x <> - 1 Then 
            Dim Redirect As String
            Redirect = e.Url.ToString().Substring((x + 1))
            If Redirect <> "" Then 
                ' Error handling code omitted in this example. 
                ' Uri constructor throws a UriFormatException if there's 
                ' an error.
                webBrowser1.Navigate(New Uri(Redirect))
            Else
                MessageBox.Show("Specify a URL")
            End If 
        End If 
    
    End Sub
    
    private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
     {
         int x;
         //  The URL contains the results of the 
         //  HTML form following the equals sign.
         x = e.Url.ToString().LastIndexOf("=");
         if (x != -1)
         {
             string Redirect;
             Redirect = e.Url.ToString().Substring(x + 1);
             if (Redirect != "")
             {
                 // Error handling code omitted in this example. 
                 // Uri constructor throws a UriFormatException if there's 
                 // an error.
                 webBrowser1.Navigate(new Uri(Redirect));
             }
             else
             {
                 MessageBox.Show("Specify a URL");
             }
         }
     }
    

    In the previous Visual Basic code example, the Navigating event handler is already associated with the control. Declare this event handler in C# as follows:

    this.webBrowser1.Navigating += new System.Windows.Forms.WebBrowserNavigatingEventHandler(this.webBrowser1_Navigating);
    

To navigate out of the WebBrowser on the Smartphone

  • The following code example sets the focus on another control when UP is pressed on the navigation key.

    The WebBrowser control uses tabbing logic from Microsoft Pocket Internet Explorer to enable the user to navigate to different links and embedded controls on the Web site shown by the control. You can override this default tabbing behavior by using the KeyPreview property.

    The following code example assumes that KeyPreview has been set to true in the form's constructor or in code that handles the Load event for the form.

    Protected Overrides Sub OnKeyDown(ByVal keyg As KeyEventArgs) 
        If keyg.KeyCode = Keys.Up Then
            textBox1.Focus()
        End If 
        MyBase.OnKeyDown(keyg)
    
    protected override void OnKeyDown(KeyEventArgs keyg)
    {
        if (keyg.KeyCode == Keys.Up)
        {
            textBox1.Focus();
        }
        base.OnKeyDown(keyg);
    }
    

Compiling the Code

This example requires references to the following namespaces:

See Also

Tasks

How to: Add Web Browser Capabilities to a Windows Forms Application

Concepts

.NET Compact Framework How-to Topics