WebBrowser.Url Proprietà

Definizione

Ottiene o imposta l'URL del documento corrente.

public:
 property Uri ^ Url { Uri ^ get(); void set(Uri ^ value); };
[System.ComponentModel.Bindable(true)]
[System.ComponentModel.TypeConverter(typeof(System.Windows.Forms.WebBrowserUriTypeConverter))]
public Uri Url { get; set; }
[System.ComponentModel.Bindable(true)]
[System.ComponentModel.TypeConverter(typeof(System.Windows.Forms.WebBrowserUriTypeConverter))]
public Uri? Url { get; set; }
[<System.ComponentModel.Bindable(true)>]
[<System.ComponentModel.TypeConverter(typeof(System.Windows.Forms.WebBrowserUriTypeConverter))>]
member this.Url : Uri with get, set
Public Property Url As Uri

Valore della proprietà

Uri

Uri che rappresenta l'URL del documento corrente.

Attributi

Eccezioni

Questa istanza di WebBrowser non è più valida.

Non è stato possibile recuperare un riferimento a un'implementazione dell'interfaccia IWebBrowser2 dal controllo ActiveX WebBrowser sottostante.

Il valore specificato al momento dell'impostazione della proprietà non è un URI assoluto. Per altre informazioni, vedere IsAbsoluteUri.

Esempio

Nell'esempio di codice seguente viene illustrato come utilizzare la Url proprietà per implementare una barra degli indirizzi per il WebBrowser controllo . In questo esempio è necessario che il form contenga un WebBrowser controllo denominato , un TextBox controllo denominato webBrowser1TextBoxAddresse un Button controllo denominato ButtonGo. Quando si digita un URL nella casella di testo e si preme INVIO o si fa clic sul pulsante Vai , il WebBrowser controllo passa all'URL specificato. Quando si naviga facendo clic su un collegamento ipertestuale, la casella di testo viene aggiornata automaticamente per visualizzare l'URL corrente.

// Navigates to the URL in the address text box when 
// the ENTER key is pressed while the text box has focus.
void TextBoxAddress_KeyDown( Object^ /*sender*/, System::Windows::Forms::KeyEventArgs^ e )
{
   if ( e->KeyCode == System::Windows::Forms::Keys::Enter &&  !this->TextBoxAddress->Text->Equals( "" ) )
   {
      this->WebBrowser1->Navigate( this->TextBoxAddress->Text );
   }
}

// Navigates to the URL in the address text box when 
// the Go button is clicked.
void ButtonGo_Click( System::Object^ /*sender*/, System::EventArgs^ /*e*/ )
{
   if (  !this->TextBoxAddress->Text->Equals( "" ) )
   {
      this->WebBrowser1->Navigate( this->TextBoxAddress->Text );
   }
}

// Updates the URL in TextBoxAddress upon navigation.
void WebBrowser1_Navigated( Object^ /*sender*/, System::Windows::Forms::WebBrowserNavigatedEventArgs^ /*e*/ )
{
   this->TextBoxAddress->Text = this->WebBrowser1->Url->ToString();
}
// 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();
}

' 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

Commenti

L'impostazione di questa proprietà equivale a chiamare il Navigate metodo e passarlo all'URL specificato.

Il WebBrowser controllo mantiene un elenco di cronologia di tutte le pagine Web visitate durante una sessione di esplorazione. Quando si imposta la Url proprietà, il WebBrowser controllo passa all'URL specificato e lo aggiunge alla fine dell'elenco di cronologia.

Il WebBrowser controllo archivia le pagine Web dai siti visitati di recente in una cache sul disco rigido locale. Ogni pagina può specificare una data di scadenza che indica per quanto tempo rimarrà nella cache. Quando il controllo passa a una pagina, consente di risparmiare tempo visualizzando una versione memorizzata nella cache, se disponibile, anziché scaricare nuovamente la pagina. Utilizzare il Refresh metodo per forzare il WebBrowser ricaricamento della pagina corrente scaricandolo, assicurandosi che il controllo visualizzi la versione più recente.

Nota

Questa proprietà contiene l'URL del documento corrente, anche se è stato richiesto un altro documento. Se si imposta il valore di questa proprietà e quindi lo si recupera immediatamente, il valore recuperato potrebbe essere diverso dal valore impostato se il WebBrowser controllo non ha avuto tempo per caricare il nuovo documento. È possibile recuperare il nuovo valore in un DocumentCompleted gestore eventi.

Si applica a

Vedi anche