65.9K
CodeProject 正在变化。 阅读更多。
Home

扩展 axWebbrowser 控件事件。

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.54/5 (11投票s)

2003年7月15日

viewsIcon

129758

downloadIcon

577

如何在 VB.NET 中扩展 axWebbrowser 的事件。

引言

我在各种 VB.NET 论坛上看到了一些关于 axWebbrowser 控件的问题(BeforeNavigate2 事件不起作用),所以我决定尝试解决这个问题,这就是结果。

使用代码

首先,将一个 axWebbrowser 控件添加到窗体中,在本例中命名为 brweb。然后,我们需要在“Windows 生成的代码”区域下添加一个 SHDocVw.DWebBrowserEvents_Event。这将成为 brWeb 的新事件处理程序。接下来,使用几行简单的代码进行测试,以展示如何处理 BeforeNavigate 事件。

Private WithEvents doc As SHDocVw.DWebBrowserEvents_Event 
     '/// doc will handle all the events for brweb now... 
     Private Sub Form1_Load(ByVal sender As System.Object, _ 
           ByVal e As System.EventArgs) Handles MyBase.Load 
         Dim b As Object = brWeb.Application 
         doc = DirectCast(b, SHDocVw.WebBrowser_V1) 
         '///set doc as the active handler for brweb's events. 
     End Sub 
  
     Private Sub Button1_Click(ByVal sender As System.Object, _ 
             ByVal e As System.EventArgs) Handles Button1.Click 
         brWeb.Navigate("http://google.com") '///lets navigate to a website. 
     End Sub 
  
     Private Sub doc_BeforeNavigate(ByVal URL As String, _ 
             ByVal Flags As Integer, ByVal TargetFrameName As String, _ 
             ByRef PostData As Object, ByVal Headers As String, _ 
             ByRef Cancel As Boolean) Handles doc.BeforeNavigate 
         MessageBox.Show(URL) '/// check that before navigate now works. 
     End Sub 
  
     Private Sub doc_StatusTextChange(ByVal _ 
             [Text] As String) Handles doc.StatusTextChange 
         Label1.Text = Text '///show the status text in a label. 
     End Sub 
  
     Private Sub doc_TitleChange(ByVal [Text] As String) _ 
                                       Handles doc.TitleChange 
         MyBase.Text = Text '/// set the form's caption to the current url 
     End Sub
© . All rights reserved.