This documentation is archived and is not being maintained.

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:

  • The Refresh method throws a NotSupportedException exception.

  • Because the WebBrowser control can contain subcontrols embedded in the HTML form, you cannot determine if the WebBrowser control has the focus by monitoring the GotFocus event. As a workaround, use a process of elimination of other controls that might have the focus.

  • The Url property is not set in WebBrowserNavigatingEventArgs and returns an empty string.

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 System.Windows.Forms.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.

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("<html><body>");
    sb.Append("<a href=");
    sb.Append("\"");
    sb.Append("http://wwww.microsoft.com");
    sb.Append("\"");
    sb.Append(">Microsoft</a><p>");
    sb.Append("Specify a URL:<br>");
    sb.Append("<form method='GET'><input type='text' name='address'/>");
    sb.Append("<br><input type='submit'>");
    sb.Append("</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 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 != "")
             {
                 webBrowser1.Navigate(new Uri(Redirect));
             }
             else
             {
                 MessageBox.Show("Specify a URL");
             }
         }
     }
    
    

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 allow 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)
        {
            textBox1.Focus();
        }
        base.OnKeyDown(keyg);
    }
    
    

Compiling the Code

This example requires references to the following namespaces:

See Also

Show: