Click to See Complete Forum and Search --> : [RESOLVED] VS2005 WebBrowser Control New Window Problem
jgags
Oct 19th, 2006, 12:14 PM
Hello,
I have an applicatin with the webbrowser control (.net not the COM component). I'm having trouble getting the URL to navigate to when the user clicks on a link that implements a pop up.
This is the code I have currently:
Private Sub WebBrowser1_NewWindow(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles WebBrowser1.NewWindow
Dim frmMe As New form()
rmMe.Show()
frmMe.WebBrowser1.Navigate(?????????)
e.Cancel = True
End Sub
I can't find any documentation about how to retrieve to URL for the destination file the user is navigating to. I have tried to get the URL from the StatusTextChanged routine like this:
Private Sub WebBrowser1_StatusTextChanged1(ByVal sender As Object, yVal As System.EventArgs) Handles WebBrowser1.StatusTextChanged
strcurrenturl = WebBrowser1.StatusText
End Sub
And then use the strcurrent variable in the navigate method in the openwindow routine, but that doesn't seem to work for every instance of a new page.
Any help would be greatly appreciated.
kleinma
Oct 19th, 2006, 12:22 PM
You can't
This is because populating a new window and including the URL was not introduced to windows until XP SP2.
If you use the COM webbrowser, you will find it has NewWindow2 and NewWindow3 events (NewWindow2 is basically equal to the 2005 .NET NewWindow one)
NewWindow3 however passes in the URL of where the popup will be navigating to.
So you can use the COM browser if you want (which is better in many ways that the 2005 one) but still it will only work in XP SP2.. My guess is if the code was run on an eariler OS than XP SP2, NewWindow3 would simply just not fire, and it would go right to fire NewWindow2.
Here is documentation (http://msdn.microsoft.com/library/default.asp?url=/workshop/browser/webbrowser/reference/events/newwindow3.asp) to back this up
jgags
Oct 19th, 2006, 12:29 PM
Thank you. That helps out a lot.
jgags
Oct 19th, 2006, 01:37 PM
OK. Now I have another problem. I switched to using the microsoft web browser com component and the code that I've seen to open a new window in the browser app is as follows:
Private Sub WebBrowser1_NewWindow2(ppDisp As Object, Cancel As Boolean)
Dim frm As Form1
Set frm = New Form1
Set ppDisp = frm.WebBrowser1.Object
frm.Show
End Sub
However, in .NET, the 'Set ppDisp = frm.WebBrowser1.Object' line of code isn't working. It's not recognizing the object method. Do you (or anyone else) have any idea how to implement this in .NET?? Any help would be greatly appreciated.
kleinma
Oct 19th, 2006, 02:00 PM
that code is VB6...
try something like this
Private Sub wb_NewWindow2(ByVal sender As Object, ByVal e As AxSHDocVw.DWebBrowserEvents2_NewWindow2Event) Handles WebBrowser1.NewWindow2
Try
e.ppDisp = frmPopupWindow.wb.Application
frmPopupWindow.Show()
Catch ex As Exception
MessageBox.Show("Error occured opening popup window")
End Try
End Sub
where frmPopupWindow is the form that will be the popup window form, and it has a webbrowser control on it called "wb"
jgags
Oct 19th, 2006, 03:00 PM
That works. Thank you so much for all of your help.
kleinma
Oct 19th, 2006, 03:29 PM
No problem. Please mark this thread resolved using the thread tools above.
armoghan
Dec 6th, 2007, 09:17 AM
you can also use the VS2005 webbrowser control
Make a class named ExtendedWebBrowser.vb and paste the code below in it..
then use ExtendedWebBrowser instead of WebBrowser and you will have a new event in it, called
NewWindowWithTaget
which has WebBrowserExtendedNavigatingEventArgs as args, you can get the target url from it
Imports System.Runtime
Imports System.ComponentModel
'Extend the WebBrowser control
Public Class ExtendedWebBrowser
Inherits WebBrowser
Private cookie As AxHost.ConnectionPointCookie
Private events As WebBrowserExtendedEvents
'This method will be called to give you a chance to create your own event sink
Protected Overloads Overrides Sub CreateSink()
'MAKE SURE TO CALL THE BASE or the normal events won't fire
MyBase.CreateSink()
events = New WebBrowserExtendedEvents(Me)
cookie = New AxHost.ConnectionPointCookie(Me.ActiveXInstance, events, GetType(DWebBrowserEvents2))
End Sub
Protected Overloads Overrides Sub DetachSink()
If cookie IsNot Nothing Then
cookie.Disconnect()
cookie = Nothing
End If
MyBase.DetachSink()
End Sub
'This new event will fire when the page is navigating
Public Event NewWindowWithTaget As EventHandler(Of WebBrowserExtendedNavigatingEventArgs)
Protected Sub OnNewWindow3(ByVal url As String, ByVal e As WebBrowserExtendedNavigatingEventArgs)
RaiseEvent NewWindowWithTaget(Me, e)
End Sub
'This class will capture events from the WebBrowser
Private Class WebBrowserExtendedEvents
Inherits System.Runtime.InteropServices.StandardOleMarshalObject
Implements DWebBrowserEvents2
Private _Browser As ExtendedWebBrowser
Public Sub New(ByVal browser As ExtendedWebBrowser)
_Browser = browser
End Sub
Public Sub NewWindow3(ByVal pDisp As Object, ByRef cancel As Boolean, ByRef flags As Object, ByRef hostURL As Object, ByRef URL As Object) Implements DWebBrowserEvents2.NewWindow3
Dim args As New WebBrowserExtendedNavigatingEventArgs(URL)
args.Cancel = cancel
_Browser.OnNewWindow3(URL, args)
cancel = args.Cancel
End Sub
End Class
<InteropServices.ComImport(), InteropServices.Guid("34A715A0-6587-11D0-924A-0020AFC7AC4D"), InteropServices.InterfaceTypeAttribute(InteropServices.ComInterfaceType.InterfaceIsIDispatch), InteropServices.TypeLibType(InteropServices.TypeLibTypeFlags.FHidden)> _
Public Interface DWebBrowserEvents2
<InteropServices.DispId(273)> _
Sub NewWindow3(ByVal pDisp As Object, ByRef cancel As Boolean, ByRef flags As Object, ByRef hostURL As Object, ByRef URL As Object)
End Interface
End Class
Public Class WebBrowserExtendedNavigatingEventArgs
Inherits CancelEventArgs
Private _Url As String
Public Sub New(ByVal url As String)
_Url = url
End Sub
Public ReadOnly Property Url() As String
Get
Return _Url
End Get
End Property
End Class
disccomp
Feb 12th, 2008, 01:58 PM
Works very well, except I send my popups to a new instance of a second form. The javascript in the popup waits 10 seconds, then does a window.close. But windows asks whether I want to close it, which I do, but it doesn't close when I click ok. I don't even want it to ask.
I found this
http://msdn2.microsoft.com/en-us/library/aa768352(VS.85).aspx
..but _WindowClosing event is missing from the extendedwebbrowser
Please advise,
-Mark
disccomp
Feb 14th, 2008, 02:32 PM
What I have so far..
Imports System.Runtime
Imports System.ComponentModel
'Extend the WebBrowser control
Public Class ExtendedWebBrowser
Inherits WebBrowser
Private cookie As AxHost.ConnectionPointCookie
Private events As WebBrowserExtendedEvents
Private Const WM_PARENTNOTIFY As Integer = &H210
Private Const WM_DESTROY As Integer = 2
'Define New event to fire
Public Event WindowClosing()
Protected Overrides Sub WndProc(ByRef m As Message)
Select Case m.Msg
Case WM_PARENTNOTIFY
If (Not DesignMode) Then
If (m.WParam = WM_DESTROY) Then
' Tell whoever cares we are closing
RaiseEvent WindowClosing()
End If
End If
DefWndProc(m)
Case Else
MyBase.WndProc(m)
End Select
End Sub
'This method will be called to give you a chance to create your own event sink
Protected Overloads Overrides Sub CreateSink()
'MAKE SURE TO CALL THE BASE or the normal events won't fire
MyBase.CreateSink()
events = New WebBrowserExtendedEvents(Me)
cookie = New AxHost.ConnectionPointCookie(Me.ActiveXInstance, events, GetType(DWebBrowserEvents2))
End Sub
Protected Overloads Overrides Sub DetachSink()
If cookie IsNot Nothing Then
cookie.Disconnect()
cookie = Nothing
End If
MyBase.DetachSink()
End Sub
'This new event will fire when the page is navigating
Public Event NewWindowWithTaget As EventHandler(Of WebBrowserExtendedNavigatingEventArgs)
Protected Sub OnNewWindow3(ByVal url As String, ByVal e As WebBrowserExtendedNavigatingEventArgs)
RaiseEvent NewWindowWithTaget(Me, e)
End Sub
'This class will capture events from the WebBrowser
Private Class WebBrowserExtendedEvents
Inherits System.Runtime.InteropServices.StandardOleMarshalObject
Implements DWebBrowserEvents2
Private _Browser As ExtendedWebBrowser
Public Sub New(ByVal browser As ExtendedWebBrowser)
_Browser = browser
End Sub
Public Sub NewWindow3(ByVal pDisp As Object, ByRef cancel As Boolean, ByRef flags As Object, ByRef hostURL As Object, ByRef URL As Object) Implements DWebBrowserEvents2.NewWindow3
Dim args As New WebBrowserExtendedNavigatingEventArgs(URL)
args.Cancel = cancel
_Browser.OnNewWindow3(URL, args)
cancel = args.Cancel
End Sub
End Class
<InteropServices.ComImport(), InteropServices.Guid("34A715A0-6587-11D0-924A-0020AFC7AC4D"), InteropServices.InterfaceTypeAttribute(InteropServices.ComInterfaceType.InterfaceIsIDispatch), InteropServices.TypeLibType(InteropServices.TypeLibTypeFlags.FHidden)> _
Public Interface DWebBrowserEvents2
<InteropServices.DispId(273)> _
Sub NewWindow3(ByVal pDisp As Object, ByRef cancel As Boolean, ByRef flags As Object, ByRef hostURL As Object, ByRef URL As Object)
End Interface
End Class
Public Class WebBrowserExtendedNavigatingEventArgs
Inherits CancelEventArgs
Private _Url As String
Public Sub New(ByVal url As String)
_Url = url
End Sub
Public ReadOnly Property Url() As String
Get
Return _Url
End Get
End Property
End Class
disccomp
Feb 14th, 2008, 04:23 PM
I found the solution!!
Add window.opener=self; to the script and windows no longer bugs you to close the window!!
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.