Solved

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

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

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

729 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