Solved

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

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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

778 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