I am trying to use the WebBrowser control to launch a new form for popups instead of it opening in IE. I have tried to use the AxWebBrowser instead to get the popups which works with NewWindow3 event and just doing e.ppDisp = AxWebBrowser.Application
, but there are many limitations that come with AxWebBrowser. So instead I am trying to Extend the normal WebBrowser to include the NewWindow3 event like the AxWebBrowser but running into problems. With e.ppDisp = AxWebBrowser.Application
I am getting errors: "InvalidVariant was detected" followed by "Specified OLE variant is invalid" if I continue.
Note: This is my first time extending a class so I could be over looking something simple. If I try just navigating to the new URL in the new window, I get java script errors from the site.
I have updated the code base on comments. Have removed the ExtendedWebBrowser class for a munch smaller and nicer version. Here is the New Code:
From the main form and a very similar BrowserPopup form -
Protected Overrides Sub OnLoad(ByVal e As EventArgs)
MyBase.OnLoad(e)
nativeBrowser = DirectCast(ExtendedWebBrowser1.ActiveXInstance, SHDocVw.WebBrowser)
AddHandler nativeBrowser.NewWindow3, AddressOf nativeBrowser_NewWindow3
AddHandler nativeBrowser.WindowClosing, AddressOf nativeBrowser_WindowClosing
End Sub
Private Sub nativeBrowser_NewWindow3(ByRef ppDisp As Object, ByRef Cancel As Boolean, ByVal dwflags As UInteger, ByVal bStrUrlContext As String, ByVal bstrUrl As String)
Dim popup = New BrowserPopup()
popup.Show(Me)
popup.browserPop.DocumentText = bStrUrlContext
ppDisp = popup.browserPop.ActiveXInstance
End Sub
Private Sub nativeBrowser_WindowClosing(ByVal IsChildWindow As Boolean, ByRef Cancel As Boolean)
MsgBox("working?") '<<<Doesn't Trigger>>>
End Sub
Protected Overrides Sub OnFormClosing(ByVal e As FormClosingEventArgs)
MyBase.OnFormClosing(e)
End Sub
Also, if it helps, here is the scripting from the page that should be able to close the popup form but just seems to deactivate the WebBrowser instead.
<table isListBtn="false" cellpadding="0" enabled="true" class="buttonBorderBlue"
cellspacing="0" border="0" onClick="if (typeof(workpaneMediator_toolbar)!='undefined')
workpaneMediator_toolbar.onSelect('CANCEL_ACTION', this)"
actionType="CLOSE_WINDOW_TYPE" id="workpaneMediator_toolbar_CANCEL_ACTIONWrapper"
nowrap><tr><td class="buttonBlueTD">
You're right that
WindowClosing
doesn't get fired for WinformsWebBrowser
Control, I confirm that. It appears to be a long-time bug in .NETWebBrowser
ActiveX hosting code, which has never been addressed. Check this post for more details and a possible workaround.Another possible workaround may be to host WebBrowser ActiveX Control directly via
AxHost
class, in which caseWindowClosing
gets fire correctly. Example:C#:
VB.NET: