System.Windows.Forms.HtmlDocument Class

Provides top-level programmatic access to an HTML document hosted by the System.Windows.Forms.WebBrowser control.

See Also: HtmlDocument Members

Syntax

public sealed class HtmlDocument

Remarks

System.Windows.Forms.HtmlDocument provides a managed wrapper around Internet Explorer's document object, also known as the HTML Document Object Model (DOM). You obtain an instance of System.Windows.Forms.HtmlDocument through the WebBrowser.Document property of the System.Windows.Forms.WebBrowser control.

HTML tags inside of an HTML document can be nested inside one another. System.Windows.Forms.HtmlDocument thus represents a document tree, whose children are instances of the System.Windows.Forms.HtmlElement class. The following code example shows a simple HTML file.

Example

<HTML>
    <BODY>
        <DIV name="Span1">Simple HTML Form</DIV>
        <FORM>
            <SPAN name="TextLabel">Enter Your Name:</SPAN>
            <INPUT type="text" size="20" name="Text1">
        </FORM>
    </BODY>
</HTML>

In this example, System.Windows.Forms.HtmlDocument represents the entire document inside the HTML tags. The BODY, DIV, FORM and SPAN tags are represented by individual System.Windows.Forms.HtmlElement objects.

There are several ways you can access the elements in this tree. Use the HtmlDocument.Body property to access the BODY tag and all of its children. The HtmlDocument.ActiveElement property gives you the System.Windows.Forms.HtmlElement for the element on an HTML page that has user input focus. All elements within an HTML page can have a name; the HtmlDocument.All collection provides access to each System.Windows.Forms.HtmlElement using its name as an index. HtmlDocument.GetElementsByTagName(string) will return an System.Windows.Forms.HtmlElementCollection of all System.Windows.Forms.HtmlElement objects with a given HTML tag name, such as DIV or TABLE. HtmlDocument.GetElementById(string) will return the single System.Windows.Forms.HtmlElement corresponding to the unique ID that you supply. HtmlDocument.GetElementFromPoint(System.Drawing.Point) will return the System.Windows.Forms.HtmlElement that can be found on the screen at the supplied mouse pointer coordinates.

You can also use the HtmlDocument.Forms and HtmlDocument.Images collection to iterate through elements that represent user input forms and graphics, respectively.

System.Windows.Forms.HtmlDocument is based on the unmanaged interfaces implemented by Internet Explorer's DHTML DOM: IHTMLDocument, IHTMLDocument2, IHTMLDocument3, and IHTMLDocument4. Only the most frequently used properties and methods on these unmanaged interfaces are exposed by System.Windows.Forms.HtmlDocument. You can access all other properties and methods directly using the HtmlDocument.DomDocument property, which you can cast to the desired unmanaged interface pointer.

An HTML document may contain frames, which are different windows inside of the System.Windows.Forms.WebBrowser control. Each frame displays its own HTML page. The HtmlWindow.Frames collection is available through the HtmlDocument.Window property. You may also use the HtmlDocument.Window property to resize the displayed page, scroll the document, or display alerts and prompts to the user.

System.Windows.Forms.HtmlDocument exposes the most common events you would expect to handle when hosting HTML pages. For events not exposed directly by the interface, you can add a handler for the event using HtmlDocument.AttachEventHandler(string, EventHandler).

HTML files may contain SCRIPT tags that encapsulate code written in one of the Active Scripting languages, such as JScript or VBScript. The HtmlDocument.InvokeScript(string) method provides for execution of properties and methods defined in a SCRIPT tag.

Note:

While most of the properties, methods, and events on System.Windows.Forms.HtmlDocument have kept the same names as they have on the unmanaged DOM, some have been changed for consistency with the dnprdnshort.

Requirements

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in System.Windows.Forms.dll)
Assembly Versions: 2.0.0.0