Solved

Embedded internet explorer; create link and capture click?

Posted on 2010-11-19
11
434 Views
Last Modified: 2012-06-27
Hey all.  I have an app in which I'm dynamically creating html webpages that essentially are custom reports based on database query results.  I'm displaying that html in an embedded IE window.

I'd like to add the functionality of having a user click a bit of information in their current report and that generates a new report.  Basically, I need to create a link, capture the click of that link and pass myself information about what exactly the user clicked.

This seems like it should be an easy thing to do, but I can't really find any examples.  Any thoughts?  Thanks!
0
Comment
Question by:JasonWilliam
  • 6
  • 5
11 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 34179792
I don't understand what you mean by "embedded IE window".  Can you give me some idea what you mean by that?  It would help to determine if it is possible to achieve what you need.
0
 

Author Comment

by:JasonWilliam
ID: 34189931
Sure.  Thanks for trying to help.

I've written a VB.net form app that has a main form.  On that main form are several options and textboxes and buttons the user can check/configure/select/type etc.  Then they click 'Go'.  Based on all their input and configuration, I pull data down from many database tables, create an HTML stream with the data formatted in a very specific way, then send that stream to a System.Windows.Forms.WebBrowser window contained within my program's form.

What I'd like to do now is, as I'm creating that HTML stream, embedd "links" within it so that when displayed in the WebBrowser window, the user can click.  Once clicked, I want to capture that click, reshuffle the options that generated the original HTML, automatically click Go for them and generate a new HTML stream for display in the same WebBrowser.  In order to do that, I need some way of capturing the click event within the WebBrowser, and getting the info on the specific link they've clicked.

Does that make more sense?
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 34191727
Ok, so you are using a WebBrowser control, with custom HTML, and you need to add some behavior to anchor elements.  I am still in the dark about your reports, and how you need anchor elements to change to create new reports.  

"Information is king"
0
 

Author Comment

by:JasonWilliam
ID: 34200596
Ok I think I can boil it down really simply:  

Forget everything I'm doing.  Lets say I have a brand new windows form app, and all this app has is a WebBrowser window on it, that displays a clickable "Hello World" on it.  When the user clicks, how do I pop up a message box that says "Hello world!"

If you can get me that far, I can take it from there.
0
 
LVL 96

Assisted Solution

by:Bob Learned
Bob Learned earned 500 total points
ID: 34202253
Boiling down is achievable!!  Attach to the onClick event for the anchor element.

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.WebBrowser1.DocumentText = "<A HREF=""#TOC"" onClick=""(alert('this will take you to the top!'))"">To the Table of Contents</A> "
    End Sub

End Class

Open in new window

0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:JasonWilliam
ID: 34207412
Ok we're getting really close!  Here's where my lack of understanding comes in...

I'm going to write psudeocode representing what I want to do.  I understand this is not valid code; what I'm asking for is to have you show me how to make it valid code.  Make sense?

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
           Dim docText As String
        docText = "<A HREF=""#TOC"" onClick=""(WebBrowserClicked("this will take you to the top!"))"">To the Table of Contents</A><p>"
        docText &= "<A HREF=""#TOC"" onClick=""(WebBrowserClicked("this will take you to the appendix!"))"">To the Appendix</A>"
        Me.WebBrowser1.DocumentText = docText

    End Sub

    Private Sub WebBrowserClicked(ByVal strWhatWasClicked As String)
        MsgBox(strWhatWasClicked)
    End Sub

End Class

Open in new window

0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 34209490
You need to write JavaScript in the HTML, and that can't interact with your code.  You would need to use the alert function in JavaScript, or write your own function to perform something else.
0
 

Author Comment

by:JasonWilliam
ID: 34227088
So you're saying there's no way to have a click inside the WebBrowswer1 control fire an event my app can capture?  
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 34227196
I was trying to say that the only way that I know of, is to use JavaScript, but that doesn't mean that what you are looking for is impossible, I just don't know the specific answer.  You might be able to sub-class the WebBrowser, and get at the underlying COM interfaces, but that would only be a guess.
0
 

Accepted Solution

by:
JasonWilliam earned 0 total points
ID: 34240533
Ok I think I've got it!  
Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.WebBrowser1.DocumentText = "<A HREF=""#TOC"">To the Table of Contents</A> "
    End Sub

    Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
        Dim olink As HtmlElement
        Dim olinks As HtmlElementCollection = WebBrowser1.Document.Links

        For Each olink In olinks
            olink.AttachEventHandler("onclick", AddressOf LinkClicked)
        Next

    End Sub

    Private Sub LinkClicked(ByVal sender As Object, ByVal e As EventArgs)
        Dim link As HtmlElement = WebBrowser1.Document.ActiveElement
        Dim url As String = link.GetAttribute("href")

        MsgBox("This was cicked: " & link.InnerText & vbCrLf & "It has these parameters: " & url)
    End Sub

End Class

Open in new window

Thanks for all the help TLO.  You pointed me in the right direction.
0
 

Author Closing Comment

by:JasonWilliam
ID: 34276730
TLO pointed me in the right direction, and I was able to figure the rest out on my own.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
This video discusses moving either the default database or any database to a new volume.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

757 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now