65.9K
CodeProject is changing. Read more.
Home

Extending the axWebbrowser control events.

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.54/5 (11 votes)

Jul 15, 2003

viewsIcon

129477

downloadIcon

577

How to extend the axWebbrowser's events in VB.NET.

Introduction

I have seen several messages on various VB.NET boards regarding problems with the axWebbrowser control (the BeforeNavigate2 event not working), so I decided to try to combat the issue and here is the result.

Using the code

Firstly, add a axWebbrowser control to a form, in this case called brweb. Then we need to add a SHDocVw.DWebBrowserEvents_Event below the "Windows generated code" area. This will be the new event handler for brWeb. Next, it's a simple test using a few lines of code to show how the BeforeNavigate event is handled.

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