locked
Webbrowser HtmlElement.AttachEventHandler event parameters are Nothing RRS feed

  • Question

  • Hi Guys,

    I have been trying to fix this problem for hours now.
    I am using VB.NET and my application has a webbrowser in which i am loading website using WebBrowser.Navigate() and in WebDocument.Completed event, I am getting the list of input HTML elements:-

    webBrowser.Document.GetElementsByTagName("input")

    and looping through it and attaching an event like this:-

    htmlElementObj.AttachEventHandler("onclick"AddressOf InputClicked)

    And my InputClicked method is like this:-

       Private Sub InputClicked  (ByVal sender AsObjectByVal e AsEventArgs)

            'Handle the input

     End Sub

    Now, when user clicks on input types in the page, the event is being called but inside this event, all I am getting is sender and e both Nothing.
    What can be my possible problem??

    I will be very glad if anyone can help me with this.

    Thanks




    • Edited by miztaken Friday, June 29, 2012 3:36 PM
    Friday, June 29, 2012 3:34 PM

Answers

  • All IE event are different you need to use the documentation to get the parameters

    You can use the  managed event HtmlDocument.Click event if you need to get the element clicked

    Private Sub Document_Click(ByVal sender As Object, ByVal e As HtmlElementEventArgs)
        
            Dim Elem As HtmlElement = WebBrowser1.Document.GetElementFromPoint(e.ClientMousePosition)
            'Do Something
        
    End Sub


    • Edited by Crazypennie Friday, June 29, 2012 8:43 PM
    • Marked as answer by miztaken Friday, June 29, 2012 8:44 PM
    Friday, June 29, 2012 8:43 PM

All replies

  • Your problem is that this event comes from the Internet explorer event sink. IE does not set (or have) any parameters on this event ...

    So, nothing you can do about it 

    http://msdn.microsoft.com/en-us/library/aa743612(v=VS.85).aspx


    Friday, June 29, 2012 4:49 PM
  • I haven't tried it but is this true for all the other events? or just onclick event?

    If its true for all the events then whats the point of having AttachEventHandler in HtmlElement?

    Is there any work around to get this working? 


    Friday, June 29, 2012 6:18 PM
  • All IE event are different you need to use the documentation to get the parameters

    You can use the  managed event HtmlDocument.Click event if you need to get the element clicked

    Private Sub Document_Click(ByVal sender As Object, ByVal e As HtmlElementEventArgs)
        
            Dim Elem As HtmlElement = WebBrowser1.Document.GetElementFromPoint(e.ClientMousePosition)
            'Do Something
        
    End Sub


    • Edited by Crazypennie Friday, June 29, 2012 8:43 PM
    • Marked as answer by miztaken Friday, June 29, 2012 8:44 PM
    Friday, June 29, 2012 8:43 PM
  • Thanks a lot. Will give it a try.
    Friday, June 29, 2012 8:44 PM
  • 5 years later, but since you know the element at the time of attaching the handler, you can pass it to the event handler using a lambda, like this post:

    htmlElementObj.AttachEventHandler("eventname", Sub() EventHandlerSub(htmlElementObj, EventArgs.Empty))
    
    Private Sub EventHandlerSub(sender As Object, e As EventArgs)
    End Sub
    just posting this here since I landed on this page looking for an answer
    • Edited by Zebrette Sunday, March 19, 2017 12:05 AM
    Sunday, March 19, 2017 12:03 AM