WebBrowser.Navigated Evento

Definición

Se produce cuando el control WebBrowser navega hasta un nuevo documento y empieza a cargarlo.

public:
 event System::Windows::Forms::WebBrowserNavigatedEventHandler ^ Navigated;
public event System.Windows.Forms.WebBrowserNavigatedEventHandler Navigated;
public event System.Windows.Forms.WebBrowserNavigatedEventHandler? Navigated;
member this.Navigated : System.Windows.Forms.WebBrowserNavigatedEventHandler 
Public Custom Event Navigated As WebBrowserNavigatedEventHandler 
Public Event Navigated As WebBrowserNavigatedEventHandler 

Tipo de evento

Ejemplos

En el ejemplo de código siguiente se muestra cómo usar un controlador para el Navigated evento para implementar una barra de direcciones para el WebBrowser control. En este ejemplo se requiere que el formulario contenga un control denominado webBrowser1, un TextBox control denominado TextBoxAddressy un Button control denominado ButtonGo.WebBrowser Al escribir una dirección URL en el cuadro de texto y presionar ENTRAR o hacer clic en el botón Ir , el WebBrowser control navega a la dirección URL especificada. Al navegar haciendo clic en un hipervínculo, el cuadro de texto se actualiza automáticamente para mostrar la dirección URL actual.

Para obtener el ejemplo de código completo, vea How to: Add Web Browser Capabilities to a Windows Forms Application.

// 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

Comentarios

El WebBrowser control navega a un nuevo documento cada vez que se establece una de las siguientes propiedades o se llama a métodos:

Controle el Navigated evento para recibir notificaciones cuando el WebBrowser control haya navegado a un nuevo documento. Cuando se produce el Navigated evento, el nuevo documento ha comenzado a cargarse, lo que significa que puede acceder al contenido cargado a través de las Documentpropiedades , DocumentTexty DocumentStream . Controle el DocumentCompleted evento para recibir notificaciones cuando el WebBrowser control termine de cargar el nuevo documento.

También puede recibir notificaciones antes de que comience la navegación controlando el Navigating evento. Controlar este evento le permite cancelar la navegación si no se han cumplido ciertas condiciones, por ejemplo, el usuario no ha rellenado completamente un formulario.

Para obtener más información sobre el manejo de eventos, consulte controlar y provocar eventos.

Se aplica a

Consulte también