Skip to content Skip to sidebar Skip to footer

Show Vb6 Forms When Click A Link In Html Page Of Webbrowser

I am working with VB6 WebBrowser, Here i need to open a vb6 form when user click any particular link of WebBrowser's link like In HTML

Solution 1:

Pick a better naming scheme like:

<ahref="#vb-showform2">Click To show VB6 Form2</a><ahref="#vb-waffles">Waffles</a>

Then intercept link clicks via the BeforeNavigate2 event, look at the url and if it matches #vb-* run your code:

Private Sub WebBrowserCtrl_BeforeNavigate2(ByVal pDisp AsObject, URL As Variant, Flags As Variant, TargetFrameName As Variant, PostData As Variant, Headers As Variant, Cancel AsBoolean)

    '// get #vb-XXX command from url
    Dim pos As Long: pos = InStrRev(URL, "#vb-")

    If pos Then
        Cancel = True '// stop default navigation

        URL = Mid$(URL, pos + 4)

        Select Case LCase$(URL)
            Case"showform2": Form2.Show
            '...
            Case "waffles":   MsgBox "Waffles."
            Case Else:        MsgBox "Unknown Command " & URL
        End Select
    End If

End Sub

Solution 2:

Instead of putting the form name inside the href attribute, I believe a better method would be to set a your owndata attribute and use that, it seems to me a much cleaner way to do such a task.

In my example, inside the href tag i'm using the classic void(0) to prevent the link navigation, otherwise your external link to VB forms could break the browser history with unexpected results.

To use the WebBrowser control, You should have already added in your VB project a reference to the Microsoft Internet Controls, what you need next is to add a reference to the Microsoft HTML Library, the type library contained inside the mshtml.tlb file.

Assuming your WebBrowser control is called "WebBrowser1", and you have three additional forms called "Form1", "Form2" and "Form3", in the form where you placed the WebBrowser control put this piece of code:

Private HTMLdoc As MSHTML.HTMLDocument

' Create a Web Page  to test the navigation '' You can skip this phase after your test are successfully executed 'Private Sub Form_Load()
    Dim HTML AsString
    WebBrowser1.Navigate "about:blank"
    HTML = "<html>"
    HTML = HTML & "<title>Open a VB Form from a Link</title>"
    HTML = HTML & "<body>"
    HTML = HTML & "<a data-vb='Form1' href='javascript:void(0)'>Click To show Form1</a>"
    HTML = HTML & "</br>"
    HTML = HTML & "<a data-vb='Form2' href='javascript:void(0)'>Click To show Form2</a>"
    HTML = HTML & "</br>"
    HTML = HTML & "<a data-vb='Form3' href='javascript:void(0)'>Click To show Form3</a>"
    HTML = HTML & "</br>"
    HTML = HTML & "</body>"
    HTML = HTML & "</html>"
    WebBrowser1.Document.Write HTML
End Sub

' This will load and show the form specified in the data-vb attribute of the link 'Private Sub WebBrowser1_BeforeNavigate2(ByVal pDisp AsObject, URL As Variant, Flags As Variant, TargetFrameName As Variant, PostData As Variant, Headers As Variant, Cancel AsBoolean)
    Dim frm As Form, FormName asStringIf Not (WebBrowser1.Document Is Nothing) Then
        Set HTMLdoc = WebBrowser1.Document
        FormName = vbNullString & HTMLdoc.activeElement.getAttribute("data-vb")
        If Not FormName = vbNullString Then    
            Set frm = Forms.Add(FormName)
            frm.Show
        End If
    End If
End Sub

An additional note:

You can get the content of the clicked link in following way:

HTMLdoc.activeElement.toString

Obviously, for all links in my test page, the result will be:

javascript:void(0) which is the same as the URL parameter of the BeforeNavigate event.

Another useful information which you can get from the HTMLDocument and wouldn't be available in the BeforeNavigate event is, for example:

HTMLdoc.activeElement.outerHTML

the result will be:

<Ahref="javascript:void(0)"data-vb="Form2">Click To show Form2</A>

Solution 3:

To do this with a button instead of a link, add the button to the document and a bit of javascript:

<inputtype="button"id="MyButton1_id"style="cursor: pointer"name=MyButton1value="Show It!"><SCRIPTLANGUAGE="VBScript">SubMyButton1_OnClick()
   location.href = "event:button1_show"EndSub</SCRIPT>

Then in the BeforeNavigate2 event:

Public Sub webBrowser_BeforeNavigate2(ByVal pDisp AsObject, URL As Variant, flags As Variant, TargetFrameName As Variant, PostData As Variant, Headers As Variant, Cancel AsBoolean)

  Select Case LCase$(URL)
    Case"event:button1_show"
      Cancel = True
      Form2.Show
    Case"event:other_stuff"'other stuff to do, etc
  End Select
End Sub

Post a Comment for "Show Vb6 Forms When Click A Link In Html Page Of Webbrowser"