views:

290

answers:

1

I am using a web browser in my current project and currently im using it in design mode to make it editable etc. The code i am currently using is:
WebBrowser.Document.DomDocument as IHTMLDocument2
What actually is an IHTMLDocument2, 3 or 4? I have also found that when identifying a current selection range in the document, the range.text.replace method is not working the same way that a string.replace does.

Can anybody explain to me the basic funtionality of the IHTMLDocuments and the IHTMLTxtRange please?

+2  A: 

IHTMLDocument is an interface which is essentially an "unbreakable" contract that represents what the object that implements it will provide.

Changing the interface when moving to a new version of the code would break that contract and in turn break the code that is relying on that contract.

Suppose you create :

public interface IMyInterface {
      public int Property1 { get;  set; }
}

A year later you need to add Property2 but you cannot change your interface. So one way around that is to create:

public interface IMyInterface2 {
    public int Property2 { get;set; }
}

and then with your old Class that is implementing IMyInterface :

public class MyObject : IMyInterface, IMyInterface2 {
    public int Property1 { get {} set {} }
    public int Property2 { get {} set {} }
}

Then you will not break the older contract but can use the new interface in code such as:

if (obj is IMyInterface) {
   Console.WriteLine(((IMyInterface)obj).Property1);

   if (obj is IMyInterface2) {
      //more
   }
}

So that is what Microsoft did. The mshtml library that IHTMLDocument is in is a COM library and COM rely's heavily on interfaces. So as the library evolved Microsoft added more and more Interfaces to expose the newer functionality/code.

IHTMLTxtRange is an interface for the more commonly used TextRange object. It exposes a bunch of functionality for parsing text "Fragments" or "Ranges".

http://www.webreference.com/js/column12/trmethods.html

Chad Grant