What's New
Q & A
Tip Jar
C# Helper...
Follow VBHelper on Twitter
MSDN Visual Basic Community
TitleDisplay information about the items below the mouse in a WebBrowser control in Visual Basic 6
DescriptionThis example shows how to display information about the items below the mouse in a WebBrowser control in Visual Basic 6.
KeywordsWebBrowser, Visual Basic, wait, load
CategoriesControls, Internet
This anonymously contributed example demonstrates a couple features of the WebBrowser control. When you click the button, the program uses the following code to navigate to the specified URL. It then loops to wait until the page is loaded.
Private Sub Command1_Click()
    On Error Resume Next
    'go to page in webpage

    WebBrowser1.Navigate2 Text1.Text
    Loop While WebBrowser1.Busy
End Sub
The control's BeforeNavigate2 and DownloadComplete event handlers keep the user informed.
Private Sub WebBrowser1_BeforeNavigate2(ByVal pDisp As _
    Object, URL As Variant, Flags As Variant, _
    TargetFrameName As Variant, PostData As Variant, _
    Headers As Variant, Cancel As Boolean)
    Me.Caption = Text1.Text & "...Loading"
End Sub

Private Sub WebBrowser1_DownloadComplete()
'I might really have to use DocumentComplete event
'instead of this one, DocumentComplete fires for each frame
'-the vb-helper page has an iframe!
    Me.Caption = WebBrowser1.LocationURL & " - complete"
    'set to the loaded web document to get the events
    Set mydom = WebBrowser1.document

End Sub
The control's OnMouseOver event handler displays information about the item below the mouse.
Private Sub mydom_onmouseover()
'main event space procedure
    On Error GoTo err
    'srcElement should be the one with the mouse over it
    'that caused this event - mouseover
    Set curElement = _

    'see if we have an object
    If Not (curElement Is Nothing) Then
        'use getattribute instead of more direct object
        ' properties
        '0 flag=Default. Performs a property search that is
        ' not case-sensitive, and returns an interpolated
        ' value if the property is found
        'the return value is usually a string, whereas the
        ' documentation seems to say string or null
        'spec says empty string
        'might be safer to use
        ' format$(curElement.getAttribute etc...
        Text2.Text = "Sourceindex=" & _
            curElement.sourceIndex    '& ":" &
            ' WebBrowser1.document.All(curElement.sourceIndex)
        Text2.Text = Text2.Text & ":width=" & _
            curElement.getAttribute("width", 0) & _
                     ":height=" & _
                         curElement.getAttribute("height", _
        Text3.Text = "href=" & _
            curElement.getAttribute("href", 0)
        'lesson for the day
        'curElement.getAttribute("href", 0) on an image is
        ' same as
        'curElement.getAttribute("src", 0)
        Text4.Text = curElement.innerText
        Text5.Text = curElement.innerHTML
    End If

    'uncomment to see dashed line box around item and the
    ' border disappear!
    '    curElement.Style.setAttribute "border", "dashed
    ' 1px #ff0000"
    '    Set curElement =
    ' WebBrowser1.document.parentWindow.event.fromElement
    '    If Not (curElement Is Nothing) Then
    '        curElement.Style.setAttribute "border", "none"
    '    End If

    Exit Sub
    MsgBox err.Description & ":" & err.Number
End Sub
Copyright © 1997-2006 Rocky Mountain Computer Consulting, Inc.   All rights reserved.