I am busy writing BHO (browser helper object) in C # and I need to bind event handlers to all onclick events on input elements. I do NOT use the built-in web browser provided by visual studio, instead I launch a new instance of Internet Explorer installed on the client PC. The problem occurs when using different versions of IE.
In IE7 and IE8, I can do this as follows:
public void attachEventHandler(HTMLDocument doc)
{
IHTMLElementCollection els = doc.all;
foreach (IHTMLElement el in els)
{
if(el.tagName == "INPUT")
{
HTMLInputElementClass inputElement = el as HTMLInputElementClass;
if (inputElement.IHTMLInputElement_type != "text" && InputElement.IHTMLInputElement_type != "password")
{
inputElement.HTMLButtonElementEvents_Event_onclick += new HTMLButtonElementEvents_onclickEventHandler(buttonElement_HTMLButtonElementEvents_Event_onclick);
}
}
}
}
This works fine, the fact is that IE6 throws an error when casting in HTMLInputElementClass, so you are forced to throw in DispHTMLInputElement:
public void attachEventHandler(HTMLDocument doc)
{
IHTMLElementCollection els = doc.all;
foreach (IHTMLElement el in els)
{
if(el.tagName == "INPUT")
{
DispHTMLInputElement inputElement = el as DispHTMLInputElement;
if (inputElement.type != "text" && inputElement.type != "password")
{
}
}
}
}
The problem is that I cannot find a way to bind the event to a DispHTMLInputElement object. Any ideas?