2

I am unable to fire an "onchange" event in mshtml. Can you please tell me what I am doing wrong here.

HTMLSelectElement element = (HTMLSelectElement)this.HTMLDocument.all.item(controlId, 0);
IHTMLElement e = element as IHTMLElement;
IHTMLDocument4 doc = e.document as IHTMLDocument4;
object dummy = null;
object eventObj = doc.CreateEventObject(ref dummy);
HTMLSelectElementClass se = element as HTMLSelectElementClass;
se.FireEvent("onchange", ref eventObj);

I am getting variable "se" as null. I got this piece of code from another link http://www.itwriting.com/phorum/read.php?3,1507

Can anyone help me with this.

Thanks, Sam

Sumesh Kuttan
  • 1,333
  • 1
  • 17
  • 40

2 Answers2

1

Runtime Callable Wrapper objects generated by COM calls like HTMLDocument.all.item can translate interface casting to QueryInterface calls. But the RCW does not know how to convert to a managed class like HTMLSelectElementClass, thus it returns null.

Instead of casting to HTMLSelectElementClass, cast to IHTMLElement3 to call fireEvent.

By the way, your code does not work in IE11 mode as document.all is deprecated. Use IHTMLDocument3::getElementById instead.

Sheng Jiang 蒋晟
  • 15,125
  • 2
  • 28
  • 46
1

I had tried all those which Sheng mentioned but didn't work.

This issue was solved by injecting javascript code for "onchange" and executing it. It worked.

Sumesh Kuttan
  • 1,333
  • 1
  • 17
  • 40