[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Web browser control and form field clicking

Posted on 1999-11-08
8
Medium Priority
?
333 Views
Last Modified: 2013-12-25
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.
0
Comment
Question by:MikeThelwall
  • 4
  • 4
8 Comments
 
LVL 4

Expert Comment

by:mandhjo
ID: 2195014
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
 

Author Comment

by:MikeThelwall
ID: 2199886
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
 
LVL 4

Accepted Solution

by:
mandhjo earned 400 total points
ID: 2200537
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 

Author Comment

by:MikeThelwall
ID: 2202445
Brilliant - and thanks for the extra explanation too. I can finish my project now at last.
0
 
LVL 4

Expert Comment

by:mandhjo
ID: 2203084
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
 

Author Comment

by:MikeThelwall
ID: 2230744
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
 
LVL 4

Expert Comment

by:mandhjo
ID: 2231494
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
 

Author Comment

by:MikeThelwall
ID: 2286317
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

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Question has a verified solution.

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

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

608 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