Export (0) Print
Expand All

WebBrowser.Url Property

Gets or sets the URL of the current document.

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

'Declaration
<BindableAttribute(True)> _
Public Property Url As Uri

Property Value

Type: System.Uri
A Uri representing the URL of the current document.

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 specified value when setting this property is not an absolute URI. For more information, see Uri.IsAbsoluteUri.

Setting this property is equivalent to calling the Navigate method and passing it the specified URL.

The WebBrowser control maintains a history list of all the Web pages visited during a browsing session. When you set the Url property, the WebBrowser control navigates to the specified URL and adds it to the end of the history list.

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.

NoteNote

This property contains the URL of the current document, even if another document has been requested. If you set the value of this property and then immediately retrieve it again, the value retrieved may be different than the value set if the WebBrowser control has not had time to load the new document. You can retrieve the new value in a DocumentCompleted event handler.

The following code example demonstrates how to use the Url property 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.


' Navigates to the URL in the address box when  
' the ENTER key is pressed while the ToolStripTextBox has focus. 
Private Sub toolStripTextBox1_KeyDown( _
    ByVal sender As Object, ByVal e As KeyEventArgs) _
    Handles toolStripTextBox1.KeyDown

    If (e.KeyCode = Keys.Enter) Then
        Navigate(toolStripTextBox1.Text)
    End If 

End Sub 

' Navigates to the URL in the address box when  
' the Go button is clicked. 
Private Sub goButton_Click( _
    ByVal sender As Object, ByVal e As EventArgs) _
    Handles goButton.Click

    Navigate(toolStripTextBox1.Text)

End Sub 

' Navigates to the given URL if it is valid. 
Private Sub Navigate(ByVal address As String)

    If String.IsNullOrEmpty(address) Then Return 
    If address.Equals("about:blank") Then Return 
    If Not address.StartsWith("http://") And _
        Not address.StartsWith("https://") Then
        address = "http://" & address
    End If 

    Try
        webBrowser1.Navigate(New Uri(address))
    Catch ex As System.UriFormatException
        Return 
    End Try 

End Sub 

' Updates the URL in TextBoxAddress upon navigation. 
Private Sub webBrowser1_Navigated(ByVal sender As Object, _
    ByVal e As WebBrowserNavigatedEventArgs) _
    Handles webBrowser1.Navigated

    toolStripTextBox1.Text = webBrowser1.Url.ToString()

End Sub

.NET Framework

Supported in: 4.5.2, 4.5.1, 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