nsdan2k
nsdan2k

Reputation: 1

Duplicate HtmlElementEventHandler events being triggered

regarding this article"HtmlElementEventHandler Delegate" http://msdn.microsoft.com/en-us/library/system.windows.forms.htmlelementeventhandler.aspx

the context menu event handler fires more than once. How do you suppress the duplicate events from triggering?

Here is my code :

Public Event DocumentCompleted As WebBrowserDocumentCompletedEventHandler

Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles    WebBrowser1.DocumentCompleted
    Dim Doc As HtmlDocument = Me.WebBrowser1.Document
    AddHandler Doc.ContextMenuShowing, New HtmlElementEventHandler(AddressOf Document_ContextMenuShowing)
    Dim htmldoc As HtmlDocument = Me.WebBrowser1.Document
End Sub

Private Sub Document_ContextMenuShowing(ByVal sender As Object, ByVal e As HtmlElementEventArgs)
    Try
        Dim doc As HtmlDocument = CType(sender, HtmlDocument)
        If doc.ActiveElement.TagName = "A" Then
            MsgBox(doc.ActiveElement.InnerHtml)
            e.ReturnValue = False
        End If
    Catch ex As Exception
    End Try
End Sub

Upvotes: 0

Views: 203

Answers (1)

volody
volody

Reputation: 7189

DocumentCompleted will fire for each frame in the web page. Use AddHandler only once in your WebBrowser1_DocumentCompleted Sub to make it fire only once.

Upvotes: 1

Related Questions