WebBrowser 控件实现地址栏

WebBrowser 控件具有多个与导航相关的属性、方法和事件。使用下面的成员可以将控件导航到特定 URL、在导航历史记录列表中向后和向前移动,还可以加载当前用户的主页和搜索页:

如果导航不成功,则显示一页指示出现的问题。使用这些成员中的任何一个进行导航都会导致在导航的不同阶段发生 NavigatingNavigatedDocumentCompleted 事件。

使用这些成员和其他成员(如 StopRefresh 方法)可以在应用程序中实现与 Internet Explorer 中的用户界面控件类似的用户界面控件。即使不希望在窗体上显示 WebBrowser 控件,某些成员也十分有用。例如,可以使用 Print 方法打印网页的最新版本,而不向用户显示该页。

使用 WebBrowser 控件还可以显示在应用程序中创建的内容或从数据库或资源文件检索的内容。使用 DocumentTextDocumentStream 属性,以字符串或数据流的形式获取或设置当前文档的内容。

还可以通过 Document 属性操作网页的内容,该属性包含一个 HtmlDocument 对象,向当前页提供对 HTML 文档对象模型 (DOM) 的托管访问。该属性与 ObjectForScripting 属性组合使用时,对在应用程序代码与网页中的动态 HTML (DHTML) 代码之间实现双向通信十分有用,使用它可以在单个用户界面中组合基于 Web 的控件和 Windows 窗体控件。在应用程序中可以使用 Document 属性调用脚本代码方法。脚本代码可以通过 window.external 对象访问应用程序,该对象是用于主机访问的内置 DOM 对象,它映射到为 ObjectForScripting 属性指定的对象。


下面的代码示例演示如何使用 WebBrowser 控件实现地址栏。此示例要求窗体包含一个名为 webBrowser1WebBrowser 控件、一个名为 TextBoxAddressTextBox 控件和一个名为 ButtonGoButton 控件。在文本框中键入 URL 并按 Enter 或单击“转到”按钮时,WebBrowser 控件会定位至指定的 URL。通过单击超链接进行定位时,文本框会自动更新以显示当前 URL。
Visual Basic:

 1' Navigates to the URL in the address box when 
 2' the ENTER key is pressed while the ToolStripTextBox has focus.
 3Private Sub toolStripTextBox1_KeyDown( _
 4    ByVal sender As ObjectByVal e As KeyEventArgs) _
 5    Handles toolStripTextBox1.KeyDown
 6
 7    If (e.KeyCode = Keys.Enter) Then
 8        Navigate(toolStripTextBox1.Text)
 9    End If
10
11End Sub

12
13' Navigates to the URL in the address box when 
14' the Go button is clicked.
15Private Sub goButton_Click( _
16    ByVal sender As ObjectByVal e As EventArgs) _
17    Handles goButton.Click
18
19    Navigate(toolStripTextBox1.Text)
20
21End Sub

22
23' Navigates to the given URL if it is valid.
24Private Sub Navigate(ByVal address As String)
25
26    If String.IsNullOrEmpty(address) Then Return
27    If address.Equals("about:blank"Then Return
28    If Not address.StartsWith("http://"And _
29        Not address.StartsWith("https://"Then
30        address = "http://" & address
31    End If
32
33    Try
34        webBrowser1.Navigate(New Uri(address))
35    Catch ex As System.UriFormatException
36        Return
37    End Try
38
39End Sub

40
41' Updates the URL in TextBoxAddress upon navigation.
42Private Sub webBrowser1_Navigated(ByVal sender As Object, _
43    ByVal e As WebBrowserNavigatedEventArgs) _
44    Handles webBrowser1.Navigated
45
46    toolStripTextBox1.Text = webBrowser1.Url.ToString()
47
48End Sub

C#:

 1// Navigates to the URL in the address box when 
 2// the ENTER key is pressed while the ToolStripTextBox has focus.
 3private void toolStripTextBox1_KeyDown(object sender, KeyEventArgs e)
 4{
 5    if (e.KeyCode == Keys.Enter)
 6    {
 7        Navigate(toolStripTextBox1.Text);
 8    }

 9}

10
11// Navigates to the URL in the address box when 
12// the Go button is clicked.
13private void goButton_Click(object sender, EventArgs e)
14{
15    Navigate(toolStripTextBox1.Text);
16}

17
18// Navigates to the given URL if it is valid.
19private void Navigate(String address)
20{
21    if (String.IsNullOrEmpty(address)) return;
22    if (address.Equals("about:blank")) return;
23    if (!address.StartsWith("http://"&&
24        !address.StartsWith("https://"))
25    {
26        address = "http://" + address;
27    }

28    try
29    {
30        webBrowser1.Navigate(new Uri(address));
31    }

32    catch (System.UriFormatException)
33    {
34        return;
35    }

36}

37
38// Updates the URL in TextBoxAddress upon navigation.
39private void webBrowser1_Navigated(object sender,
40    WebBrowserNavigatedEventArgs e)
41{
42    toolStripTextBox1.Text = webBrowser1.Url.ToString();
43}


posted @ 2006-12-21 20:48  齐心  Views(3536)  Comments(0Edit  收藏  举报