WebBrowser.Navigated 事件

定義

發生於 WebBrowser 控制項已巡覽至新文件且已開始載入新文件時。

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 

事件類型

範例

下列程式碼範例示範如何使用 事件的處理常式 Navigated 來實作 控制項的 WebBrowser 網址列。 此範例會要求表單包含 WebBrowser 名為 的 webBrowser1 控制項、 TextBox 名為 TextBoxAddress 的控制項,以及 Button 名為 的 ButtonGo 控制項。 當您在文字方塊中輸入 URL,然後按 ENTER 鍵或按一下 [移至 ] 按鈕時,控制項會 WebBrowser 巡覽至指定的 URL。 當您按一下超連結流覽時,文字方塊會自動更新以顯示目前的 URL。

如需完整的程式碼範例,請參閱如何:將網頁瀏覽器功能新增至 Windows Forms 應用程式

// 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 就會巡覽至新的檔:

Navigated處理事件,以在控制項巡覽至新檔時接收通知 WebBrowserNavigated事件發生時,新檔已開始載入,這表示您可以透過 DocumentDocumentTextDocumentStream 屬性存取載入的內容。 DocumentCompleted處理事件,以在控制項完成載入新檔時接收通知 WebBrowser

您也可以在流覽開始之前接收通知,方法是處理 Navigating 事件。 處理此事件可讓您取消流覽,例如,使用者尚未完整填寫表單。

如需處理事件的詳細資訊,請參閱 處理和引發事件

適用於

另請參閱