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

[This documentation is for preview only, and is subject to change in later releases. Blank topics are included as placeholders.]

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 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.

    StringBuilder sb = new StringBuilder();
    sb.Append("<a href=");
    sb.Append("Specify a URL:<br>");
    sb.Append("<form action=''><input type='text' name='address'/>");
    sb.Append("<br><input type='submit'>");
    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 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));
                 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 override void OnKeyDown(KeyEventArgs keyg)
        if (keyg.KeyCode == Keys.Up)

This example requires references to the following namespaces: