Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 474
  • Last Modified:

Embedded internet explorer; create link and capture click?

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
JasonWilliam
Asked:
JasonWilliam
  • 6
  • 5
2 Solutions
 
Bob LearnedCommented:
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
 
JasonWilliamAuthor Commented:
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
 
Bob LearnedCommented:
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
JasonWilliamAuthor Commented:
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
 
Bob LearnedCommented:
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
 
JasonWilliamAuthor Commented:
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
 
Bob LearnedCommented:
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
 
JasonWilliamAuthor Commented:
So you're saying there's no way to have a click inside the WebBrowswer1 control fire an event my app can capture?  
0
 
Bob LearnedCommented:
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
 
JasonWilliamAuthor Commented:
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
 
JasonWilliamAuthor Commented:
TLO pointed me in the right direction, and I was able to figure the rest out on my own.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now