?
Solved

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

Posted on 2007-10-08
6
Medium Priority
?
3,928 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
0
Comment
Question by:peterborchers
  • 2
  • 2
5 Comments
 
LVL 22

Expert Comment

by:RedKelvin
ID: 20167698
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
 

Author Comment

by:peterborchers
ID: 20168063
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
 
LVL 22

Expert Comment

by:RedKelvin
ID: 20168475
Excellent, does that mean you got it working?
0
 

Author Comment

by:peterborchers
ID: 20168512
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
 
LVL 1

Accepted Solution

by:
Computer101 earned 0 total points
ID: 20526496
PAQed with points refunded (125)

Computer101
EE Admin
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Following on from our article on "The Murky World of Consent and opt in", we thought we would issue some helpful guidance, not only on consent itself but knowing what information you are capturing, what you are doing with this data and how you can p…
This Micro Tutorial will demonstrate how to add subdomains to your content reports. This can be very importing in having a site with multiple subdomains.
Shows how to create a shortcut to site-search Experts Exchange using Google in the Chrome browser. This eliminates the need to type out site:experts-exchange.com whenever you want to search the site. Launch the Search Engine Menu: In chrome, via you…

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question