Export (0) Print
Expand All

WebBrowser.Navigate Method (Uri)

Loads the document at the location indicated by the specified Uri into the WebBrowser control, replacing the previous document.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

public void Navigate(
	Uri url
)

Parameters

url
Type: System.Uri

A Uri representing the URL of the document to load.

ExceptionCondition
ObjectDisposedException

This WebBrowser instance is no longer valid.

InvalidOperationException

A reference to an implementation of the IWebBrowser2 interface could not be retrieved from the underlying ActiveX WebBrowser control.

ArgumentException

The url parameter value does not represent an absolute URI. For more information, see Uri.IsAbsoluteUri.

The WebBrowser control maintains a history list of all the Web pages visited during a browsing session. When you call this overload, the WebBrowser control navigates to the specified URI and adds it to the end of the history list. Use the GoBack method to return the control to a previous page in the navigation history. Use the GoForward method to return to a later page in the navigation history after navigating backward.

The WebBrowser control stores Web pages from recently visited sites in a cache on the local hard disk. Each page can specify an expiration date indicating how long it will remain in the cache. When the control navigates to a page, it saves time by displaying a cached version, if one is available, rather than downloading the page again. Use the Refresh method to force the WebBrowser control to reload the current page by downloading it, ensuring that the control displays the latest version.

You can use the Navigate method to implement an address bar similar to the one in Internet Explorer.

The following code example demonstrates how to use the Navigate method to implement an address bar for the WebBrowser control. This example requires that your form 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 a 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();
}

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft