Solved

Give focus to an AxWebBrowserControl

Posted on 2007-11-29
14
651 Views
Last Modified: 2010-08-05
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.
0
Comment
Question by:DColin
  • 9
  • 4
14 Comments
 
LVL 18

Expert Comment

by:armoghan
ID: 20381863
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
 

Author Comment

by:DColin
ID: 20386548
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
 
LVL 18

Expert Comment

by:armoghan
ID: 20394079
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:DColin
ID: 20418269
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
 
LVL 18

Expert Comment

by:armoghan
ID: 20418786
:) 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
 
LVL 18

Expert Comment

by:armoghan
ID: 20418819
By the way how do you do it in axWebBrowser control.. if you send some code.. it will be of some help
0
 
LVL 18

Expert Comment

by:armoghan
ID: 20418904
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
 

Author Comment

by:DColin
ID: 20419083
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
 
LVL 18

Expert Comment

by:armoghan
ID: 20419211
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
 
LVL 18

Expert Comment

by:armoghan
ID: 20419311
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
 
LVL 18

Accepted Solution

by:
armoghan earned 500 total points
ID: 20420010
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
 

Author Comment

by:DColin
ID: 20426711
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
 
LVL 18

Expert Comment

by:armoghan
ID: 20428383
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
 

Expert Comment

by:lcha
ID: 25465433
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question