How do I add an event listener to my button created in C# for IE BHO?
In my IE BHO I create a input button element using:
var button = doc.createElement("input");
button.setAttribute("value", "myButton"); //next line gets an error
button.addEventListener("click", openFunction, false); //openFunction is a function inside of the same class
When I try to call button.addEventListener I get a 'mshtml.IHTMLElement' does not contain a definition for 'addEventListener' error. I find thi开发者_如何学Pythons odd because according to this site (http://help.dottoro.com/ljeuqqoq.php) I should be in the clear.
I also saw this thread but it seems like overkill for what I'm trying to do and I can't get it to work.
Finally got it to work. Had to use similar method to the thread I linked in original question.
/// Inside the BHO.cs file and in my namespace
/// Generic HTML DOM Event method handler.
///
public delegate void DHTMLEvent(IHTMLEventObj e);
///
/// Generic Event handler for HTML DOM objects.
/// Handles a basic event object which receives an IHTMLEventObj which
/// applies to all document events raised.
///
public class DHTMLEventHandler
{
public DHTMLEvent Handler;
HTMLDocument Document;
public DHTMLEventHandler(HTMLDocument doc)
{
this.Document = doc;
}
[DispId(0)]
public void Call()
{
Handler(Document.parentWindow.@event);
}
}
public void BrowserEventHandler(IHTMLEventObj e)
{
try
{
if (e.type == "click" && e.srcElement.id == "IDOfmyButton")
{
// do something.
something();
}
}
catch
{
}
}
Inside my OnDocumentComplete method (also in same namespace as above [extra info for novices]):
DHTMLEventHandler myHandler = new DHTMLEventHandler(framedoc);
myHandler.Handler += new DHTMLEvent(this.BrowserEventHandler);
button.onclick = myHandler;
A lot of work just to get a button to click. In firefox it was one line :O
精彩评论