Solved

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

Posted on 2010-11-30
3
594 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
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…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

895 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

14 Experts available now in Live!

Get 1:1 Help Now