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
 
There is no "mousemove" Html event, it's "onmousemove". For hover you probably want the onmouseover event.
 
i have tried both your suggestions but still not working.

Does it matter that im calling two different events in the same loop - should i seperate them!

All i would like to do in the first instance is just emulate the action like in any browser where you put your curser over a link and it shows the target in the status bar!

thanks for your help
 
You can do that by handling the StatusTextChanged event of the WebBrowser control, where you display the StatusText property value.
 
without being daft i thought thats what i was doing but to know that the mouse was over a link and to call the event i put it on the AttachEventHandler!

Is there a better way to do it?
 
without being daft i thought thats what i was doing
So where is your code for StatusTextChanged event ??
 
correction, i was being daft!

But i changed my code to include the status Change but it still doesnt work!

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

            Olink.AttachEventHandler("onmouseover", AddressOf LinkStatus)
        Next


    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 = mainWebBrowser.StatusText & "(Allowed)"
        Else

            ToolStripStatusLabel2.Text = mainWebBrowser.StatusText & "(not allowed)"

        End If
    End Sub

thanks for your help

regards
 
I see you write "not working" several times, but I have no idea what you mean by that. What is not working?
About what it appears you are doing, I think you are going the wrong way about this. Are you trying to do this:
  1. display hovered url in a status control?
  2. prevent navigation to urls you don't approve?
If so, solutions:
  1. JohnH said:
    You can do that by handling the StatusTextChanged event of the WebBrowser control, where you display the StatusText property value.
  2. Handle the Navigating event of the WebBrowser control, where you use the event parameter to check Url and set Cancel as appropriate.
 
hi

i have got it to cancel naivgating to urls ok in a seperate function, but simply i just want the url to display in the status bar when the mouse cursor hovers over it - i have used the StatusText (dont seem to have StatusTextChanged) and it is not displaying in the status bar - wondered if you could see where i have gone wrong?

many thanks
 
WebBrowser control has a StatusTextChanged event. You're not using the WebBrowser control? Since you're using the DocumentCompleted event you must be.
 
im using visual studio 2008 and my web control only has StatusText.

I can get the effect i want if i use the timer like this:

VB.NET:
Expand Collapse Copy
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

        ToolStripStatusLabel2.Text = mainWebBrowser.StatusText

    End Sub

What i want to do is use the mainWebBrowser.StatusText event and pass it as a string variable into my function so i can check to see if it is valid - i can get it to work once the link has been clicked but i want to display in the status bar if it is valid or not before it gets clicked. but i cannot get it to call the event just on mouseover, only on a click does it seem to work!

Apologies if i havent been explaining properly but it seems that it should be easy but is proving otherwise.

thanks for your help

regards
 
ok, got it working (mostly)

i had to use the timer event to give me the ability to have the call on a seperate sub based on which link the cursor is over (if that makes sense)

my code is below, maybe not perfect but seems to do the job so i can refine it now

VB.NET:
Expand Collapse Copy
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

        ToolStripStatusLabel2.Text = mainWebBrowser.StatusText

        TestURL = mainWebBrowser.StatusText

        If InStr(TestURL, "http") Then 'check that it is a link in focus
            testlink(TestURL)
        End If

    End Sub


Sub testlink(ByVal checklink As String)
        Accepted = False

        Dim url As String = checklink

        Call CheckLinkClicked(url)

        'Call SearchValue()

        If Accepted = True Then

            ToolStripStatusLabel2.Text = mainWebBrowser.StatusText & "(Allowed)"

        Else

            ToolStripStatusLabel2.Text = mainWebBrowser.StatusText & "(not allowed)"

        End If

End Sub

    Private Sub CheckLinkClicked(ByVal ClickedURL As String) ', ByVal testurl As String

        Dim i As Integer
        Dim x As Integer = 0
        Dim TotalRows As Integer
        Dim CurrentRow As String
        Dim TempOriginalURL As String
        Dim modifiedURL As String

        CheckURL(ClickedURL)'strip the link to check

        TempOriginalURL = ClickedURL

        If ClickedURL = String.Empty Then
            If TestURL = String.Empty Then
                x = 1
            End If
        Else
            TotalRows = ISurfDataSet.Urls.Rows.Count
            TempOriginalURL = ClickedURL
            For i = 0 To TotalRows - 1
                CurrentRow = ISurfDataSet.Urls.Rows(i).Item(0).ToString

                modifiedURL = TempOriginalURL.Remove(Module1.OriginalURLCount)

                If InStr(CurrentRow, modifiedURL) Then
                    x = x + 1
                End If

                'MsgBox(modifiedURL)
            Next
        End If

        If x > 0 Then
            Accepted = True
        End If

    End Sub

many thanks for your help on this

kind regards
 
Use the StatusTextChanged event where you use StatusText property. You have somehow managed to use the DocumentCompleted event, now it's time to use the StatusTextChanged event.
Your CheckLinkClicked is a Boolean function method by the way, not a sub method as you've implemented it.
 
of course, i see what you mean - sorry - looking for it as a property not an event!!!

have moved the code from the timer to the StatusTextChanged and working the same but using a little less resource i spose as only being used when needed!

VB.NET:
Expand Collapse Copy
    Private Sub mainWebBrowser_StatusTextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles mainWebBrowser.StatusTextChanged
        ToolStripStatusLabel2.Text = mainWebBrowser.StatusText

        TestURL = mainWebBrowser.StatusText

        If InStr(TestURL, "http") Then 'check that it is a link in focus
            testlink(TestURL)
        End If
    End Sub

thanks for your help and persistance!
 
Hurray! Here is some other code suggestions that you may or may not want to consider. Since StatusText reports also other messages that users would want to know like "loading" "waiting" you can use the Uri class to check what is what, it has a TryCreate function that will parse the text and tell you if successful without throwing an exception. It is also useful in this context to know that StatusText always reports absolute urls. Example:
VB.NET:
Expand Collapse Copy
Private Sub WebBrowser1_StatusTextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles WebBrowser1.StatusTextChanged
    Dim status As String = Me.WebBrowser1.StatusText
    Dim url As Uri = Nothing
    If Uri.TryCreate(status, UriKind.Absolute, url) Then
        If AllowedURL(url) Then
            Me.Status.Text = status & " [Allowed]"
        Else
            Me.Status.Text = status & " [Not Allowed]"
        End If
    Else
        Me.Status.Text = status
    End If
End Sub
The AllowedURL function can do any kind of authorization, the most common filter is to check only the host part of the Uri, ie the domain:
VB.NET:
Expand Collapse Copy
Private Function AllowedURL(ByVal url As Uri) As Boolean
    Return url.Host.EndsWith(".com")
End Function
The Navigating event already provide a Uri and can use the same AllowedURL function:
VB.NET:
Expand Collapse Copy
Private Sub WebBrowser1_Navigating(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserNavigatingEventArgs) Handles WebBrowser1.Navigating
    e.Cancel = Not AllowedURL(e.Url)
End Sub
 
Back
Top