• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4050
  • Last Modified:

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

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
0
peterborchers
Asked:
peterborchers
  • 2
  • 2
1 Solution
 
RedKelvinCommented:
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
0
 
peterborchersAuthor 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);
}
0
 
RedKelvinCommented:
Excellent, does that mean you got it working?
0
 
peterborchersAuthor 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.
0
 
Computer101Commented:
PAQed with points refunded (125)

Computer101
EE Admin
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now