WebBrowser.Navigated 事件
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
在 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个名为 的控件、一TextBox个名为 webBrowser1
的TextBoxAddress
控件和一Button个名为 的ButtonGo
控件。 在文本框中键入 URL 并按 Enter 或单击“ 转到 ”按钮时, WebBrowser 控件将导航到指定的 URL。 通过单击超链接进行导航时,文本框会自动更新以显示当前 URL。
有关完整的代码示例,请参阅如何:向 Windows 窗体 应用程序添加 Web 浏览器功能。
// 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 在 WebBrowser 控件导航到新文档时接收通知。 Navigated事件发生时,新文档已开始加载,这意味着可以通过 、 DocumentText和 DocumentStream 属性访问加载的内容Document。 处理 事件, DocumentCompleted 以在 WebBrowser 控件完成加载新文档时接收通知。
还可以通过处理 Navigating 事件在导航开始之前接收通知。 处理此事件可让你在未满足某些条件的情况下取消导航,例如,用户尚未完全填写表单。
有关处理事件的详细信息,请参阅 处理和引发事件。