Search code examples
c#internet-explorershdocvw

How to pass a parameter from SHDocVw.InternetExplorer event to C# application


I'm using

SHDocVw.InternetExplorer ie = new SHDocVw.InternetExplorer()

At some point I need to trigger an event in my C# application from Internet Explorer. I'm doing that like this:

 var eventObj = btn.GetType()
                .GetEvents()
                .FirstOrDefault(e => e.Name == "HTMLElementEvents_Event_onclick");

            if (eventObj != null) 
                eventObj.AddEventHandler(btn, new HTMLElementEvents_onclickEventHandler(GenericDomEvent));

This works OK, but I need to pass some parameter with this event and I'm not able to figure out how to do that. May be there is a way to call C# method from Internet Explorer? Please advise.


Solution

  • After many hours of banging head against the wall I came up with a solution. I'm using different event:

      var eventObj = btn.GetType()
                    .GetEvents()
                    .FirstOrDefault(e => e.Name == "HTMLElementEvents2_Event_onclick");
    
                if (eventObj != null) 
                    eventObj.AddEventHandler(btn, new HTMLElementEvents2_onclickEventHandler(GenericDomEvent));
    

    And here is the event handler:

        private bool GenericDomEvent(IHTMLEventObj e)
        {            
            var element = e.srcElement;
            var command = element.title;
            var param = element.innerText;
            //Do whatever you want...
        }
    

    The idea of this solution is to put the data into DOM element and trigger click event. In C# app you are listening for click event and as soon as event raised you reading your data from the element. Probably not the most elegant solution, but it works!