Solved

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

Posted on 2010-11-30
3
588 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
  • 2
3 Comments
 
LVL 32

Accepted Solution

by:
Erick37 earned 500 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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

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…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

747 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

13 Experts available now in Live!

Get 1:1 Help Now