WebBrowser.Navigated WebBrowser.Navigated WebBrowser.Navigated WebBrowser.Navigated Event

定义

WebBrowser 控件导航到新文档并开始加载该文档时发生。Occurs when the WebBrowser control has navigated to a new document and has begun loading it.

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

示例

下面的代码示例演示如何使用的处理程序Navigated事件来实现的地址栏WebBrowser控件。The following code example demonstrates how to use a handler for the Navigated event to implement an address bar for the WebBrowser control. 此示例要求你的窗体包含WebBrowser控件称为webBrowser1、 一个TextBox控件调用TextBoxAddress,和一个Button控件调用ButtonGoThis example requires that your form contains a WebBrowser control called webBrowser1, a TextBox control called TextBoxAddress, and a Button control called ButtonGo. 当在文本框中键入的 URL 并按 ENTER 或单击按钮,WebBrowser控件导航到指定的 URL。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. 导航时通过单击超链接,在文本框中将会自动更新以显示当前的 URL。When you navigate by clicking a hyperlink, the text box automatically updates to display the current URL.

有关完整的代码示例,请参阅如何:将 Web 浏览器功能添加到 Windows 窗体应用程序For the complete code example, see 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

注解

WebBrowser设置以下属性之一,或调用方法时,控件导航到新文档:The WebBrowser control navigates to a new document whenever one of the following properties is set or methods is called:

处理Navigated事件以接收通知时WebBrowser控件已导航到新文档。Handle the Navigated event to receive notification when the WebBrowser control has navigated to a new document. Navigated发生事件时,新的文档已开始加载,这意味着您可以访问已加载的内容经过DocumentDocumentText,和DocumentStream属性。When the Navigated event occurs, the new document has begun loading, which means you can access the loaded content through the Document, DocumentText, and DocumentStream properties. 处理DocumentCompleted事件以接收通知时WebBrowser控件完成加载新文档。Handle the DocumentCompleted event to receive notification when the WebBrowser control finishes loading the new document.

您还可以收到通知,通过处理导航开始前Navigating事件。You can also receive notification before navigation begins by handling the Navigating event. 处理此事件,可取消导航,如果不满足某些条件,例如,用户未完全填写窗体。Handling this event lets you cancel navigation if certain conditions have not been met, for example, the user has not completely filled out a form.

有关处理事件的详细信息,请参阅处理和引发事件For more information about handling events, see Handling and Raising Events.

安全性

SecurityPermission
对于直接调用方使用此控件。for immediate callers to use this control. 要求值: LinkDemand;[NIB:命名权限集](https://msdn.microsoft.com/library/08250d67-c99d-4ab0-8d2b-b0e12019f6e3): FullTrustDemand value: LinkDemand; [NIB: Named Permission Sets](https://msdn.microsoft.com/library/08250d67-c99d-4ab0-8d2b-b0e12019f6e3): FullTrust.

适用于

另请参阅