Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Extending the axWebbrowser control events.

0.00/5 (No votes)
14 Jul 2003 1  
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

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here