AttachEventHandler issues

ninjaimp

Well-known member
Joined
Jun 13, 2007
Messages
80
Programming Experience
1-3
i have a web browser control which i have added a status strip.

I can get the status strip to display the clicked link fine but i would like it to display the link target when the mouse is hover over the link but i cannot get it to work!

My code is below - any help is greatly appreciated!


VB.NET:
Expand Collapse Copy
Private Sub mainWebBrowser_DocumentCompleted(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles mainWebBrowser.DocumentCompleted



        Dim Olink As HtmlElement
        Dim Elem As HtmlElementCollection = mainWebBrowser.Document.Links

        'check the link that has been clicked
        For Each Olink In Elem
            'call the procedure to check if this is a valid link
            Olink.AttachEventHandler("onclick", AddressOf LinkClicked)
            Olink.AttachEventHandler("mousemove", AddressOf LinkStatus)
        Next

    End Sub



    Private Sub LinkClicked()

        Accepted = False

        Dim link As HtmlElement = mainWebBrowser.Document.ActiveElement
        Dim url As String = link.GetAttribute("href")

        Call CheckLinkClicked(url)

        'Call SearchValue()

        If Accepted = True Then
            mainWebBrowser.Navigate(url)
            ToolStripStatusLabel2.Text = url & "(Allowed)"
        Else
            bCancel = True
            ToolStripStatusLabel2.Text = url & "(not allowed)"
            MsgBox("That is not an allowed URL" & Chr(13) & Chr(10) & "If this is wrong, then someone needs to enter this site into the allowed list!")
        End If

        urlTextBox.Text = mainWebBrowser.Url.ToString

    End Sub


    Sub LinkStatus()

        Accepted = False

        Dim link As HtmlElement = mainWebBrowser.Document.ActiveElement
        Dim url As String = link.GetAttribute("href")

        Call CheckLinkClicked(url)

        'Call SearchValue()

        If Accepted = True Then

            ToolStripStatusLabel2.Text = url & "(Allowed)"
        Else

            ToolStripStatusLabel2.Text = url & "(not allowed)"

        End If
    End Sub
   Private Sub mainWebBrowser_DocumentCompleted(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles mainWebBrowser.DocumentCompleted



        Dim Olink As HtmlElement
        Dim Elem As HtmlElementCollection = mainWebBrowser.Document.Links

        'check the link that has been clicked
        For Each Olink In Elem
            'call the procedure to check if this is a valid link
            Olink.AttachEventHandler("onclick", AddressOf LinkClicked)
            Olink.AttachEventHandler("mousemove", AddressOf LinkStatus)
        Next

    End Sub



    Private Sub LinkClicked()

        Accepted = False

        Dim link As HtmlElement = mainWebBrowser.Document.ActiveElement
        Dim url As String = link.GetAttribute("href")

        Call CheckLinkClicked(url)

        'Call SearchValue()

        If Accepted = True Then
            mainWebBrowser.Navigate(url)
            ToolStripStatusLabel2.Text = url & "(Allowed)"
        Else
            bCancel = True
            ToolStripStatusLabel2.Text = url & "(not allowed)"
            MsgBox("That is not an allowed URL" & Chr(13) & Chr(10) & "If this is wrong, then someone needs to enter this site into the allowed list!")
        End If

        urlTextBox.Text = mainWebBrowser.Url.ToString

    End Sub


    Sub LinkStatus()

        Accepted = False

        Dim link As HtmlElement = mainWebBrowser.Document.ActiveElement
        Dim url As String = link.GetAttribute("href")

        Call CheckLinkClicked(url)

        'Call SearchValue()

        If Accepted = True Then

            ToolStripStatusLabel2.Text = url & "(Allowed)"
        Else

            ToolStripStatusLabel2.Text = url & "(not allowed)"

        End If
    End Sub
 
About your earlier attempts, I only browsed that code quickly, but I have some input to that also for if you would need to use such element events again or just want to know more about that code. You used ActiveElement with onmouseover, but hovering an element will not activate it, one way to get the element in this case could be GetElementFromPoint method. A better alternative to link.AttachEventHandler ("onmouseover") would be AddHandler link.MouseOver, the HtmlElement class provides managed events for some of the html events, the event handlers in this case use same pattern as other .Net events (sender object, e event arguments) where sender is the html object and e provides other useful info. To prevent navigation this way, you would have used the links managed Click event to set the event ReturnValue to False.

Another thing with DocumentCompleted event is that it is raised for various stages of the load process of the document, which means that any code you do there has effect multiple times. Here adding an event handler would cause the handler method to be called equal number of times for each event raised. That can be handled by checking the ReadyState of the browser to see that it has reached complete state before acting, like this:
VB.NET:
Expand Collapse Copy
Private Sub WebBrowser1_DocumentCompleted(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
    If Me.WebBrowser1.[B]ReadyState[/B] = WebBrowserReadyState.[B]Complete[/B] Then
        'here you can add event handler once for the event
 
Back
Top