Export (0) Print
Expand All

WebBrowser Class

Enables the user to navigate Web pages inside your form.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

[ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)] 
[ComVisibleAttribute(true)] 
public class WebBrowser : WebBrowserBase
/** @attribute ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch) */ 
/** @attribute ComVisibleAttribute(true) */ 
public class WebBrowser extends WebBrowserBase
ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch) 
ComVisibleAttribute(true) 
public class WebBrowser extends WebBrowserBase
Not applicable.

The WebBrowser control lets you host Web pages and other browser-enabled documents in your Windows Forms applications. You can use the WebBrowser control, for example, to provide integrated HTML-based user assistance or Web browsing capabilities in your application. Additionally, you can use the WebBrowser control to add your existing Web-based controls to your Windows Forms client applications.

NoteImportant:

The WebBrowser control is resource-intensive. Be sure to call the Dispose method when you are finished using the control to ensure that all resources are released in a timely fashion. You must call the Dispose method on the same thread that attached the events, which should always be the message or user-interface (UI) thread.

The WebBrowser control cannot be used by partially trusted code. For more information, see Using Libraries from Partially Trusted Code.

The WebBrowser control has several properties, methods, and events related to navigation. The following members let you navigate the control to a specific URL, move backward and forward through the navigation history list, and load the home page and search page of the current user:

If the navigation is unsuccessful, a page indicating the problem is displayed. Navigation with any of these members causes the Navigating, Navigated, and DocumentCompleted events to occur at different stages of navigation.

These and other members, such as the Stop and Refresh methods, let you implement user interface controls in your application similar to those in Internet Explorer. Some members are useful even when you do not want to display the WebBrowser control on your form. For example, you can use the Print method to print the latest version of a Web page without displaying the page to the user.

The WebBrowser control also lets you display content that you create in your application or you retrieve from a database or resource file. Use the DocumentText or DocumentStream property to get or set the contents of the current document as a string or data stream.

You can also manipulate the contents of a Web page through the Document property, which contains an HtmlDocument object that provides managed access to the HTML document object model (DOM) for the current page. This property is useful, when used in combination with the ObjectForScripting property, to implement two-way communication between your application code and dynamic HTML (DHTML) code in a Web page, letting you combine Web-based controls and Windows Forms controls in a single user interface. You can use the Document property to call scripting code methods from your application. Your scripting code can access your application through the window.external object, which is a built-in DOM object provided for host access, and which maps to the object that you specify for the ObjectForScripting property.

NoteNote:

This class makes security demands at the class level. A SecurityException is thrown when a derived class or any caller in the call stack does not have full trust permission. For details about security demands, see Link Demands and Inheritance Demands.

NoteNote:

The WebBrowser class can only be used in threads set to single thread apartment (STA) mode. To use this class, ensure that your Main method is marked with the STAThreadAttribute attribute.

Windows Mobile 2003 for Pocket PC, Windows Mobile 2003 for Smartphone, Windows CE Platform Note: Full functionality for the WebBrowser in .NET Compact Framework applications requires Windows Mobile version 5.0 software for Pocket PCs and Smartphones. For more information, see How to: Use a WebBrowser Control.

The following code example demonstrates how to implement an address bar for use with the WebBrowser control. This example requires that you have a form that contains a WebBrowser control called webBrowser1, a TextBox control called TextBoxAddress, and a Button control called ButtonGo. When you type a URL into the text box and press ENTER or click the Go button, the WebBrowser control navigates to the URL specified. When you navigate by clicking a hyperlink, the text box automatically updates to display the current URL.

For the complete code example, see How to: Add Web Browser Capabilities to Your Windows Forms Application.

// Navigates to the URL in the address box when 
// the ENTER key is pressed while the ToolStripTextBox has focus.
private void toolStripTextBox1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Enter)
    {
        Navigate(toolStripTextBox1.Text);
    }
}

// Navigates to the URL in the address box when 
// the Go button is clicked.
private void goButton_Click(object sender, EventArgs e)
{
    Navigate(toolStripTextBox1.Text);
}

// Navigates to the given URL if it is valid.
private void Navigate(String address)
{
    if (String.IsNullOrEmpty(address)) return;
    if (address.Equals("about:blank")) return;
    if (!address.StartsWith("http://") &&
        !address.StartsWith("https://"))
    {
        address = "http://" + address;
    }
    try
    {
        webBrowser1.Navigate(new Uri(address));
    }
    catch (System.UriFormatException)
    {
        return;
    }
}

// Updates the URL in TextBoxAddress upon navigation.
private void webBrowser1_Navigated(object sender,
    WebBrowserNavigatedEventArgs e)
{
    toolStripTextBox1.Text = webBrowser1.Url.ToString();
}

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0

.NET Compact Framework

Supported in: 2.0

Community Additions

ADD
Show:
© 2014 Microsoft