We help IT Professionals succeed at work.

How to catch onpropertychange event for DIV HTML element in C# BHO

4,392 Views
Last Modified: 2013-12-08
Hi.

I've created a generic DOM event handler in my C# BHO code as follows:

-----
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.

[ComVisible(true)]
public class DHTMLEventHandler
{
   public DHTMLEvent Handler;
   HTMLDocument Document;

   public DHTMLEventHandler(mshtml.HTMLDocument doc)
   {
      this.Document = doc;
   }

    [DispId(0)]
    public void Call()
    {
       Handler(Document.parentWindow.@event);
     }
}
----

I've used the code above to sucessfully create an event handler for the onclick event of the document element of the current webbrowser.

I now want to create an event handler to capture when the innerHTML property of a DIV tag contained in the document changes. I've done the following:

--------
IHTMLDocument3 doc2 = (IHTMLDocument3)((IWebBrowser2)pDisp).Document;
DHTMLEventHandler Handler2 = new DHTMLEventHandler(doc2 as mshtml.HTMLDocument);
Handler2.Handler += new DHTMLEvent(OnDocumentPropertyChanged);
doc2.onpropertychange = Handler2;
--------

I've then coded the event handler as follows:

---------
public void OnDocumentPropertyChanged(IHTMLEventObj evo)
{
    IHTMLElement2 element = (IHTMLElement2)evo.srcElement;
           
     if (evo != null)
     {
        MessageBox.Show(evo.srcElement.innerHTML);
      }
}
---------
This code seems to capture when a property changes, but it doesn't correctly reflect the source element that raised the event.

Can anyone help me with identifying the source element that raised the onpropertychanged event or suggest another way of capturing the change of a child DIV tags inner HTML property.

Thanks,
Peter
Comment
Watch Question

Hi Peter, the DIV element, does not have an OnPropertyChange event, that is why you cannot capture it.

http://www.w3schools.com/tags/tag_div.asp

RedK

Author

Commented:
I don't think that's correct as I subsequently got the DIV element by its id and then sucessfully wired up the onpropertychange event.
---------------
IHTMLDocument3 doc2 = (IHTMLDocument3)((IWebBrowser2)pDisp).Document;

if ((IHTMLElement2)doc2.getElementById("articleFrame")!=null)
 {
   DIVElement = (IHTMLElement2)doc2.getElementById("articleFrame");
     ((mshtml.HTMLElementEvents2_Event)DIVElement).onpropertychange += new  HTMLElementEvents2_onpropertychangeEventHandler(OnDocumentPropertyChanged);
}
Excellent, does that mean you got it working?

Author

Commented:
I abandoned the idea of catching the onpropertychange event in the Document and then working out whether the child element that triggered the event was the DIV I'm interested in. Instead I referenced the DIV element itself and wired its onpropertychange event. This meant the logic had to change slighly but ulitmately I can do what I orginally set out to do.

So yes, I guess it means I got it working.
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.