重复HtmlElementEventHandler事件被触发

问题描述:

关于本文的“HtmlElementEventHandler代表” http://msdn.microsoft.com/en-us/library/system.windows.forms.htmlelementeventhandler.aspx重复HtmlElementEventHandler事件被触发

上下文菜单事件处理程序触发不止一次。你如何抑制触发重复事件?

这里是我的代码:

公共事件DocumentCompleted作为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 

DocumentCompleted将火在网页中每一帧。在您的WebBrowser1_DocumentCompleted Sub中只使用AddHandler一次,使其只触发一次。