Web browser control and form field clicking

Is there any way with the web browser control to respond to the user clicking on a form field in a web page? Ideally I would like to write some code so that when a form field is clicked on, the form field is sent to a procedure as an HTMLAnchorElement object.

If there is not a direct way, I would be happy with some kind of polling routine searching for which field has the focus.

I can't use a DHTML solution because it has to work for thousands of other people's web pages.
MikeThelwallAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mandhjoCommented:
This is kind of possible, but not directly how you described.

I had some code lieing around from answering a previous question so I modified it a little bit and came up with the following.  My app is a VB 5 application that uses the Microsoft Web Browser control.

1. I navigate the browser control to a particular URL in response to a button click.
2. When the DocumentComplete event is fired from the control to my VB app (I set up the WithEvents stuff previously), I then iterate through the body of the document and look for a particular Anchor Element (in my case, the first anchor that has "html" in its href attribute).
3.  I had previously declared (dimmed with events) a global variable of type HTMLAnchorElement.  At this point, I assign the element found in step 2 to this variable.
4.  I had previously added a handler for the onclick event of the global variable referred to in step 3 and added a msgbox statement to it to see if it would be called.

Lo and behold, when I clicked on that link, my handler was called and the browser's handler WAS NOT called.  When I clicked on other anchor elements, my handler was not called.

This isn't the exact fulfillment your question, so I'll only post the code if you really want to see it.  Perhaps it will spark some other thought in your brain that will answer your question.  Let me know if you want the code.

The challenge for you will be to handle the onclick event for *every* anchor element in the body of the document.

0
MikeThelwallAuthor Commented:
Thanks a lot, that sounds perfect, a really clever solution. Please post the code.

I had given up on being able to solve this problem and thought that it was impossible.
0
mandhjoCommented:
Maybe not the cleanest code, but the idea is there.

Global Variables

Option Explicit
Dim g_oActDoc As Object
Dim WithEvents g_oDocBody As HTMLBody
Dim g_oActDoc2 As IHTMLDocument2
Dim WithEvents g_oDesiredAnchorElement As HTMLAnchorElement

'browser' is my Web Browser Control on my form

Private Sub browser_DocumentComplete(ByVal pDisp As Object, URL As Variant)
    On Error Resume Next
    Set g_oActDoc = browser.Document
    If (Err.Number <> 0) Then
        MsgBox "Error getting document object - " & Err.Description
    Else
        Set g_oActDoc2 = g_oActDoc
        If (Err.Number <> 0) Then
            MsgBox "Error getting document2 element from active document - " & Err.Description
        Else
            Set g_oDocBody = g_oActDoc2.body
            If (Err.Number <> 0) Then
                MsgBox "Error getting body element from active document2 - " & Err.Description
            Else
                g_oDocBody.bgColor = Int((32000 * Rnd) + 1)
                Dim strBody As String
                strBody = g_oDocBody.outerText
                If (Err.Number <> 0) Then
                    MsgBox "Error getting body text - " & Err.Description
                Else
                    txtBody.Text = strBody
                End If
               
                Dim oAnchorElement As HTMLAnchorElement
                Dim nCount As Integer
                nCount = 0
                For Each oAnchorElement In g_oDocBody.All
                    If (InStr(1, oAnchorElement.href, "html", vbTextCompare) <> 0) Then
                        If (nCount = 0) Then
                            Set g_oDesiredAnchorElement = oAnchorElement
                        End If
                        MsgBox oAnchorElement.href
                        nCount = nCount + 1
                    End If
                Next
            End If
        End If
    End If
End Sub

Private Function g_oDesiredAnchorElement_onclick() As Boolean
    MsgBox "Clicked the desired anchor element!"
End Function

In Summary, I tell the browser to navigate to a page in response to a button click (code not shown).  When the DocumentComplete event is fired, I rifle through the document's body looking for the first anchor element that has 'html' in its href attribute.  When I find it, I assign my global variable to that element (g_oDesiredAnchorElement).  Since I have dimmed g_oDesiredAnchorElement with events, I added a handler for g_oDesiredAnchorElement_onclick.  This handler is fired when the desired anchor element is clicked.

Good luck.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

MikeThelwallAuthor Commented:
Brilliant - and thanks for the extra explanation too. I can finish my project now at last.
0
mandhjoCommented:
I just looked through the code again.  There is a line in there that changes the background color to a random number.  


g_oDocBody.bgColor = Int((32000 * Rnd) + 1)

This was left over from some monkeying around I was doing.  Obviously, you don't need it, but I'm sure you already figured that out.
0
MikeThelwallAuthor Commented:
Problems - the code is excellent and works fine on links but does not seem to transfer to form fields. I've tried it with various form element and VB is happy to assign the withevents object to a form field but does not seem to recognise the click event - I get the javascript click event on the page instead. I've tried the focus event too. I'm guessing that VB does not yet have the functionality to deal properly with form fields(?) - if so, I'll have to wait for it, I guess.  I'm still happy with the code though - it is obviously the 'correct' way to do it.
0
mandhjoCommented:
Have you tried removing the javascript click event?  Perhaps only one handler is allowed and the javascript one takes precedence.  I may have some time today to fool around with this...I'll see what I can do, but don't get your hopes up.

I don't know where you are in the world, but if you celebrate Thanksgiving...have a good one.

Mark
0
MikeThelwallAuthor Commented:
I've tried loads of things now and given up - guess that you didn't have any more luck?
I've written the codee that should work and will wait until Microsoft catch up!!! In the meantime I am using a workaround.
Thanks again for the help. I'm in the UK where we only hear about Thanksgiving on the Simpsons - but I was in Houston ten years ago and remember the Pumpkin pie!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.