Results 1 to 11 of 11

Thread: [RESOLVED] VS2005 WebBrowser Control New Window Problem

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2004
    Posts
    54

    Resolved [RESOLVED] VS2005 WebBrowser Control New Window Problem

    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:

    VB Code:
    1. Private Sub WebBrowser1_NewWindow(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles WebBrowser1.NewWindow
    2.  
    3.  Dim frmMe As New form()
    4.  rmMe.Show()
    5.  frmMe.WebBrowser1.Navigate(?????????)
    6.   e.Cancel = True
    7.  
    8. 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:

    VB Code:
    1. Private Sub WebBrowser1_StatusTextChanged1(ByVal sender As Object, yVal As System.EventArgs) Handles WebBrowser1.StatusTextChanged
    2.  
    3. strcurrenturl = WebBrowser1.StatusText
    4.  
    5. 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.

  2. #2
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: VS2005 WebBrowser Control New Window Problem

    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 to back this up

  3. #3

    Thread Starter
    Member
    Join Date
    Jul 2004
    Posts
    54

    Re: VS2005 WebBrowser Control New Window Problem

    Thank you. That helps out a lot.

  4. #4

    Thread Starter
    Member
    Join Date
    Jul 2004
    Posts
    54

    Re: VS2005 WebBrowser Control New Window Problem

    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:

    VB Code:
    1. Private Sub WebBrowser1_NewWindow2(ppDisp As Object, Cancel As Boolean)
    2. Dim frm As Form1
    3. Set frm = New Form1
    4. Set ppDisp = frm.WebBrowser1.Object
    5. frm.Show
    6. 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.

  5. #5
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: VS2005 WebBrowser Control New Window Problem

    that code is VB6...

    try something like this

    VB Code:
    1. Private Sub wb_NewWindow2(ByVal sender As Object, ByVal e As AxSHDocVw.DWebBrowserEvents2_NewWindow2Event) Handles WebBrowser1.NewWindow2
    2.         Try
    3.             e.ppDisp = frmPopupWindow.wb.Application
    4.             frmPopupWindow.Show()
    5.         Catch ex As Exception
    6.             MessageBox.Show("Error occured opening popup window")
    7.         End Try
    8.     End Sub

    where frmPopupWindow is the form that will be the popup window form, and it has a webbrowser control on it called "wb"

  6. #6

    Thread Starter
    Member
    Join Date
    Jul 2004
    Posts
    54

    Re: VS2005 WebBrowser Control New Window Problem

    That works. Thank you so much for all of your help.

  7. #7
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: VS2005 WebBrowser Control New Window Problem

    No problem. Please mark this thread resolved using the thread tools above.

  8. #8
    New Member
    Join Date
    Dec 2007
    Posts
    1

    Re: [RESOLVED] VS2005 WebBrowser Control New Window Problem

    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

  9. #9
    New Member
    Join Date
    Feb 2008
    Posts
    5

    Re: [RESOLVED] VS2005 WebBrowser Control New Window Problem

    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/lib...52(VS.85).aspx

    ..but _WindowClosing event is missing from the extendedwebbrowser

    Please advise,
    -Mark

  10. #10
    New Member
    Join Date
    Feb 2008
    Posts
    5

    Re: [RESOLVED] VS2005 WebBrowser Control New Window Problem

    What I have so far..

    Code:
    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

  11. #11
    New Member
    Join Date
    Feb 2008
    Posts
    5

    Re: [RESOLVED] VS2005 WebBrowser Control New Window Problem

    I found the solution!!

    Add window.opener=self; to the script and windows no longer bugs you to close the window!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width