Greg
Greg

Reputation: 10788

Can't set IHTMLEventObj2::fromElement

I'm trying to generate synthetic Javascript events in an Internet Explorer extension, and I'm having trouble getting the fromElement property to stick. Here's an excerpt of my code:

MsHtml.IHTMLDocument4 doc4 = ... // the document object
Object o = null;
MsHtml.IHTMLEventObj2 eObj = 
    (MsHtml.IHTMLEventObj2)doc4.CreateEventObject(ref o);

// string that specifies the from element, e.g. "document.getElementById('id1')":
string locator = ... 
object from = doc4.Script.GetType().InvokeMember("eval", 
                                                 BindingFlags.InvokeMethod, 
                                                 null, 
                                                 doc4.Script, 
                                                 new object[] { locator });

// from now holds a ref to an object that implements the IHTMLElement interface
eObj.fromElement = from;
IHTMLElement el = eObj.fromElement;
// el == null

What am I doing wrong here? eObj.fromElement should be equal to from, but it doesn't seem to be getting set.

Upvotes: 33

Views: 1401

Answers (2)

outcoldman
outcoldman

Reputation: 11832

To find the right element you should try method getElementById from IHTMLDocument6: http://msdn.microsoft.com/en-us/library/cc288667

Upvotes: 1

Luke Baughan
Luke Baughan

Reputation: 4686

Just a wild shot in the dark but could it be because your passing a "null" object to the CreateEventObject method? What about if you change this:

Object o = null;

To this:

Object o = new Object();

On line 3 of your example?

Upvotes: 1

Related Questions