Give focus to an AxWebBrowserControl

Hi experts,

I am trying to give focus to an AxWebBrowser control so I can the use sendkey. AxWebBrowser1.focus() and AxWebBrowser1.select() do not seem to be giving the control focus. Thanks.
DColinAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
armoghanConnect With a Mentor Commented:
Ok here is your solution

Using 2nd approach.

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

Open in new window

0
 
armoghanCommented:
When you use naviagte to for WebBrowser it automatically takes your focus to it..

If you are working with .NET 2.0, then instead of using AxWebBrowser you can use WebBrowser which is quite stable and Select works in it
0
 
DColinAuthor Commented:
Hi armoghan,

"When you use naviagte to for WebBrowser it automatically takes your focus to it.. "

This works for the Net 2.0 WebBrowser control but not for the COM AxWebBrowser control. The COM control has the focus() and select() methods but they do not seem to work. I am using the COM control over the Net 2.0 control because of frame access limitations with the Net 2.0 control.
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
armoghanCommented:
I have never found any problem in gettng Frame in WebBrowser control as it is actually a wrapper on AxWebBrowser with some of the bugs elminated

Please check my previous post about frame
http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/Q_21956182.html
0
 
DColinAuthor Commented:
Hi armoghan,

I have taken a look at the link you provided and am impressed by you knowledge of the DOM. If you can solve my frame access problem I will happily drop the COM control in favor of the NET2.0 control.

How do I access a frame contained in a new window 'popped' by a link. I am able to use the NewWindow2 method with the COM control but the NET2.0 control does not seem to be able to do it, thanks.
0
 
armoghanCommented:
:) i do not have much knowledge on DOM

two suggestions
1. You can use WebBrowser1.ActiveXInstance which will give you activeX instance. so kind of you are using both
2. NewWindow event is present in webbrowser control, you can use that as well
0
 
armoghanCommented:
By the way how do you do it in axWebBrowser control.. if you send some code.. it will be of some help
0
 
armoghanCommented:
i dont know the exect thing what you are trying to do ..

But Two more ways.

1. If you are not showing popup in a IE window
Then handle the link click event and cancel it
Sometjhing like

Private Sub WebBrowser1_DocumentCompleted(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
        Dim anchor As HtmlElement = WebBrowser1.Document.GetElementById("x")
        AddHandler anchor.Click, AddressOf anchor_Click
    End Sub

    Private Sub anchor_Click(ByVal sender As Object, ByVal e As System.Windows.Forms.HtmlElementEventArgs)
        e.ReturnValue = False ' not to popup
        'ctype(sender,HtmlElement) ' this is the element clicked,
    End Sub

Now get href and open the url yourself in a webbrowser control and then you can get everything about it.

2. Way 2
Check how i have got the contents of IE window in the following example
http://www.codeproject.com/vb/net/ByPassAutomation.asp
0
 
DColinAuthor Commented:
Hi armoghan,

This is how I access the popup code.


'Form1
 
Private Sub AxWebBrowser1_NewWindow2(ByVal sender As Object, ByVal e As AxSHDocVw.DWebBrowserEvents2_NewWindow2Event) Handles AxWebBrowser1.NewWindow2
 
        Dim frmWB As Form2
        frmWB = New Form2()
 
        frmWB.AxWebBrowser1.RegisterAsBrowser = True
        e.ppDisp = frmWB.AxWebBrowser1.Application
        frmWB.Visible = True
 
    End Sub
 
'Form2
 
Private Sub AxWebBrowser1_DocumentComplete(ByVal sender As Object, ByVal e As AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEvent) Handles AxWebBrowser1.DocumentComplete
 
        Dim HTMLStr As String = AxWebBrowser1.Document.Body.InnerHtml
 
End sub

Open in new window

0
 
armoghanCommented:
Ok this is my Start.html and on link click htmlWithFrames.html opens up
<html>
<head>
</head>
<body>
<a name="X" href="htmlWithFrames.html" target="_blank">link text</a>
</body>
</html>


and this is how i have coded to get the inner html of frame

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        WebBrowser1.Navigate("C:\Documents and Settings\username\Desktop\FrameExample\Start.html")
    End Sub
    Private Sub WebBrowser1_DocumentCompleted(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
        If boolFramedHtml Then
            Dim HTMLStr As String = WebBrowser1.Document.Body.InnerHtml
            ' Here you have
        Else
            Dim anchor As HtmlElement = WebBrowser1.Document.GetElementById("X")
            AddHandler anchor.Click, AddressOf anchor_Click
        End If
    End Sub
    Dim boolFramedHtml As Boolean
    Private Sub anchor_Click(ByVal sender As Object, ByVal e As System.Windows.Forms.HtmlElementEventArgs)
        If CType(sender, HtmlElement).Name = "X" Then
            e.ReturnValue = False ' not to popup
            boolFramedHtml = True
            Me.WebBrowser1.Navigate(CType(sender, HtmlElement).GetAttribute("href").ToString)
        End If
    End Sub
0
 
armoghanCommented:
two more ways to do the same thing..

1. Similar to the above
Private Sub WebBrowser1_NewWindow(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles WebBrowser1.NewWindow
        Me.WebBrowser1.Navigate(WebBrowser1.StatusText)
    End Sub

2. Proper way, in case the pop up is opened using javascript etc
http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=115195
0
 
DColinAuthor Commented:
Hi armoghan,

Thankyou for your help. You have given me more than I need to continue with my project. When you look at how easy it is to access popup source from an AxWebBrowser Control do you think it is way too complicated using the NET2.0 control?
0
 
armoghanCommented:
WebBrowser 2.0 = Bug  fixed in AxBrowser + Some Feautures not implemented but can be added the same way Microsoft is doing it. + No more unmanaged component
 
0
 
lchaCommented:
Any possible way I can get some assistance.   I have been struggling with using this extended webbrowser class for over a week and not getting anywhere.

I'm trying to figure out  how to get the URL from the window popup.  The approach I'm taking from this solution.

I am using ExtendedWebBrowser instead of WebBrowser.   There is a event in it, called
NewWindowWithTaget which has WebBrowserExtendedNavigatingEventArgs as args.
URL is a property of "WebBrowserExtendedNavigatingEventArgs" class which is an argument of Event, "NewWindowWithTaget"


How would I get the target url from it?    I am a newbie with VB .net so I really am needing some guidance here possibly a code sample?

I posted the question below but no one has been able to help so far.    If you can provide any insight it would be extremely appreciated as I have totally hit a wall.    

http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/Q_24752666.html#a25465406
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.