Chad said:
Hi
In vb.net if you add a webbrowser control the windowclosing event does not
fire. I have searched everywhere for a solution rather I get more people
saying I have the same problem.
I found this...Could someone please convert this to VB.NET to give us a
glimmer of hope OR provide a solution.
If u implement axWebBrowser, you'll find the WindowClosing event doesn't
fire. This is the work around, which I confirm works.
1. Right below the System.Windows.Forms.Form class add another class
whichderives from SHDocVw.DWebBrowserEvents2. For example:\
public class IEEvents: SHDocVw.DWebBrowserEvents2
{}
2. Save the file and go to class view (View | Class View menu option). Go to
IEEvents class in the tree view and expand it. Keep expanding its children
till you see 'DWebBrowserEvents'. Right click and select 'Add | Implement
interfaces' menu option.
3. A method for WindowClosing event should be generated by above step. Apply
the 'DispId' attribute to the method as shown below:
[DispId(0x00000107)]
public void WindowClosing(bool IsChildWindow, ref bool Cancel)
{
//message box to the event handler works
MessageBox.Show("Closing Event", "IE", MessageBoxButtons.OK,
MessageBoxIcon.Exclamation);
}
4. Add the following lines of code to the end of the Forms
'InitializeComponent' method.
UCOMIConnectionPointContainer pConPtCon =
(UCOMIConnectionPointContainer)this.axWebBrowser1.GetOcx();
UCOMIConnectionPoint pConPt;
Guid guid = typeof(SHDocVw.DWebBrowserEvents2).GUID;
pConPtCon.FindConnectionPoint(ref guid, out pConPt);
IEEvents e = new IEEvents();
//make sure you declare private int dwCookie in the form class but outside
this method
pConPt.Advise(e, out dwCookie);
5. Add the following lines of code to the beginning of the Forms Close
handler method.
UCOMIConnectionPointContainer pConPtCon =
(UCOMIConnectionPointContainer)this.axWebBrowser1.GetOcx();
UCOMIConnectionPoint pConPt;
Guid guid = typeof(SHDocVw.DWebBrowserEvents2).GUID;
pConPtCon.FindConnectionPoint(ref guid, out pConPt);
pConPt.Unadvise(dwCookie);
URL:
http://www.kbcafe.com/iBLOGthere4iM/?guid=20040501150250
Thank-you and regards
Chad
Here's tthe code in VB.Net:
Option Explicit On
Option Strict On
Imports System.Runtime.InteropServices
Public Class Form1
Inherits System.Windows.Forms.Form
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
Dim pConPtCon As UCOMIConnectionPointContainer =
CType(Me.AxWebBrowser1.GetOcx(), UCOMIConnectionPointContainer)
Dim pConPt As UCOMIConnectionPoint
Dim guid As Guid = GetType(SHDocVw.DWebBrowserEvents2).GUID
pConPtCon.FindConnectionPoint(guid, pConPt)
Dim ieEvent As New WebBrowserEvents
pConPt.Advise(ieEvent, dwCookie)
End Sub
Dim dwCookie As Integer = -1
#Region " Windows Form Designer generated code "
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents AxWebBrowser1 As AxSHDocVw.AxWebBrowser
Friend WithEvents Button1 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Dim resources As System.Resources.ResourceManager = New
System.Resources.ResourceManager(GetType(Form1))
Me.AxWebBrowser1 = New AxSHDocVw.AxWebBrowser
Me.Button1 = New System.Windows.Forms.Button
CType(Me.AxWebBrowser1,
System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'AxWebBrowser1
'
Me.AxWebBrowser1.Enabled = True
Me.AxWebBrowser1.Location = New System.Drawing.Point(24, 24)
Me.AxWebBrowser1.OcxState =
CType(resources.GetObject("AxWebBrowser1.OcxState"),
System.Windows.Forms.AxHost.State)
Me.AxWebBrowser1.Size = New System.Drawing.Size(300, 150)
Me.AxWebBrowser1.TabIndex = 0
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(96, 216)
Me.Button1.Name = "Button1"
Me.Button1.TabIndex = 1
Me.Button1.Text = "Close Ax"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(360, 286)
Me.Controls.Add(Me.Button1)
Me.Controls.Add(Me.AxWebBrowser1)
Me.Name = "Form1"
Me.Text = "Form1"
CType(Me.AxWebBrowser1,
System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
End Sub
#End Region
Protected Overrides Sub OnClosed(ByVal e As System.EventArgs)
Dim pConPtCon As UCOMIConnectionPointContainer =
CType(Me.AxWebBrowser1.GetOcx(), UCOMIConnectionPointContainer)
Dim pConPt As UCOMIConnectionPoint
Dim guid As Guid = GetType(SHDocVw.DWebBrowserEvents2).GUID
pConPtCon.FindConnectionPoint(guid, pConPt)
Dim ieEvent As New WebBrowserEvents
pConPt.Unadvise(dwCookie)
End Sub
End Class
Class WebBrowserEvents
Implements SHDocVw.DWebBrowserEvents2
<DispId(263)> _
Public Sub WindowClosing(ByVal IsChildWindow As Boolean, ByRef
Cancel As Boolean) Implements SHDocVw.DWebBrowserEvents2.WindowClosing
MessageBox.Show("IE is closing!")
End Sub
Public Sub BeforeNavigate2(ByVal pDisp As Object, ByRef URL As
Object, ByRef Flags As Object, ByRef TargetFrameName As Object, ByRef
PostData As Object, ByRef Headers As Object, ByRef Cancel As Boolean)
Implements SHDocVw.DWebBrowserEvents2.BeforeNavigate2
End Sub
Public Sub ClientToHostWindow(ByRef CX As Integer, ByRef CY As
Integer) Implements SHDocVw.DWebBrowserEvents2.ClientToHostWindow
End Sub
Public Sub CommandStateChange(ByVal Command As Integer, ByVal
Enable As Boolean) Implements SHDocVw.DWebBrowserEvents2.CommandStateChange
End Sub
Public Sub DocumentComplete(ByVal pDisp As Object, ByRef URL As
Object) Implements SHDocVw.DWebBrowserEvents2.DocumentComplete
End Sub
Public Sub DownloadBegin() Implements
SHDocVw.DWebBrowserEvents2.DownloadBegin
End Sub
Public Sub DownloadComplete() Implements
SHDocVw.DWebBrowserEvents2.DownloadComplete
End Sub
Public Sub FileDownload(ByRef Cancel As Boolean) Implements
SHDocVw.DWebBrowserEvents2.FileDownload
End Sub
Public Sub NavigateComplete2(ByVal pDisp As Object, ByRef URL As
Object) Implements SHDocVw.DWebBrowserEvents2.NavigateComplete2
End Sub
Public Sub NavigateError(ByVal pDisp As Object, ByRef URL As
Object, ByRef Frame As Object, ByRef StatusCode As Object, ByRef Cancel
As Boolean) Implements SHDocVw.DWebBrowserEvents2.NavigateError
End Sub
Public Sub NewWindow2(ByRef ppDisp As Object, ByRef Cancel As
Boolean) Implements SHDocVw.DWebBrowserEvents2.NewWindow2
End Sub
Public Sub NewWindow3(ByRef ppDisp As Object, ByRef Cancel As
Boolean, ByVal dwFlags As System.UInt32, ByVal bstrUrlContext As String,
ByVal bstrUrl As String) Implements SHDocVw.DWebBrowserEvents2.NewWindow3
End Sub
Public Sub OnFullScreen(ByVal FullScreen As Boolean) Implements
SHDocVw.DWebBrowserEvents2.OnFullScreen
End Sub
Public Sub OnMenuBar(ByVal MenuBar As Boolean) Implements
SHDocVw.DWebBrowserEvents2.OnMenuBar
End Sub
Public Sub OnQuit() Implements SHDocVw.DWebBrowserEvents2.OnQuit
End Sub
Public Sub OnStatusBar(ByVal StatusBar As Boolean) Implements
SHDocVw.DWebBrowserEvents2.OnStatusBar
End Sub
Public Sub OnTheaterMode(ByVal TheaterMode As Boolean) Implements
SHDocVw.DWebBrowserEvents2.OnTheaterMode
End Sub
Public Sub OnToolBar(ByVal ToolBar As Boolean) Implements
SHDocVw.DWebBrowserEvents2.OnToolBar
End Sub
Public Sub OnVisible(ByVal Visible As Boolean) Implements
SHDocVw.DWebBrowserEvents2.OnVisible
End Sub
Public Sub PrintTemplateInstantiation(ByVal pDisp As Object)
Implements SHDocVw.DWebBrowserEvents2.PrintTemplateInstantiation
End Sub
Public Sub PrintTemplateTeardown(ByVal pDisp As Object) Implements
SHDocVw.DWebBrowserEvents2.PrintTemplateTeardown
End Sub
Public Sub PrivacyImpactedStateChange(ByVal bImpacted As Boolean)
Implements SHDocVw.DWebBrowserEvents2.PrivacyImpactedStateChange
End Sub
Public Sub ProgressChange(ByVal Progress As Integer, ByVal
ProgressMax As Integer) Implements SHDocVw.DWebBrowserEvents2.ProgressChange
End Sub
Public Sub PropertyChange(ByVal szProperty As String) Implements
SHDocVw.DWebBrowserEvents2.PropertyChange
End Sub
Public Sub SetSecureLockIcon(ByVal SecureLockIcon As Integer)
Implements SHDocVw.DWebBrowserEvents2.SetSecureLockIcon
End Sub
Public Sub StatusTextChange(ByVal Text As String) Implements
SHDocVw.DWebBrowserEvents2.StatusTextChange
End Sub
Public Sub TitleChange(ByVal Text As String) Implements
SHDocVw.DWebBrowserEvents2.TitleChange
End Sub
Public Sub UpdatePageStatus(ByVal pDisp As Object, ByRef nPage As
Object, ByRef fDone As Object) Implements
SHDocVw.DWebBrowserEvents2.UpdatePageStatus
End Sub
Public Sub WindowSetHeight(ByVal Height As Integer) Implements
SHDocVw.DWebBrowserEvents2.WindowSetHeight
End Sub
Public Sub WindowSetLeft(ByVal Left As Integer) Implements
SHDocVw.DWebBrowserEvents2.WindowSetLeft
End Sub
Public Sub WindowSetResizable(ByVal Resizable As Boolean)
Implements SHDocVw.DWebBrowserEvents2.WindowSetResizable
End Sub
Public Sub WindowSetTop(ByVal Top As Integer) Implements
SHDocVw.DWebBrowserEvents2.WindowSetTop
End Sub
Public Sub WindowSetWidth(ByVal Width As Integer) Implements
SHDocVw.DWebBrowserEvents2.WindowSetWidth
End Sub
End Class