2013-02-25 148 views
0

關於本文的「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 

回答

1

DocumentCompleted將火在網頁中每一幀。在您的WebBrowser1_DocumentCompleted Sub中只使用AddHandler一次,使其只觸發一次。