?
Solved

WebBrowser Control Capturing Click Event:  What am I doing wrong?

Posted on 2010-11-30
3
Medium Priority
?
614 Views
Last Modified: 2012-06-21
Hey guys.  I have a WinForm VB.net app with a WebBrowser control defined on it, with the following code:
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=""#Link1"">First link</A><br> "
        doctext &= "<A HREF=""#Link2"">Second link</A> "
        Me.WebBrowser1.DocumentText = doctext
    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

This works fine, except when I click a second time (doesn't matter which link).  I get 2 message boxes.  Click a third time and I get 3.  And so on.  

Can you please explain why, and tell me how to only get one box each time I click a link?  Thanks!
0
Comment
Question by:JasonWilliam
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 32

Accepted Solution

by:
Erick37 earned 2000 total points
ID: 34241767
Every time the page reloads when you click a link, it adds another event handler.

You must remove them first, then readd...

            For Each olink In olinks
                  olink.DetachEventHandler("onclick", AddressOf LinkClicked)
                  olink.AttachEventHandler("onclick", AddressOf LinkClicked)
            Next
0
 
LVL 32

Expert Comment

by:Erick37
ID: 34241869
Perhaps it's better to attach only once when you load the document:


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		Me.Show()

		Dim doctext As String
		doctext = "<A HREF=""#Link1"">First link</A><br> "
		doctext &= "<A HREF=""#Link2"">Second link</A> "
		Me.WebBrowser1.DocumentText = doctext

		Application.DoEvents()

		While WebBrowser1.ReadyState <> WebBrowserReadyState.Complete
			Threading.Thread.Sleep(10)
		End While

		Dim olink As HtmlElement
		Dim olinks As HtmlElementCollection = WebBrowser1.Document.Links

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

END Sub

Open in new window

0
 

Author Closing Comment

by:JasonWilliam
ID: 34242139
That'll do it.  Thanks Erick!
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses
Course of the Month15 days, 6 hours left to enroll

771 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