This documentation is archived and is not being maintained.

HtmlElement Class

Represents an HTML element inside of a Web page.


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

[PermissionSetAttribute(SecurityAction.LinkDemand, Name = "FullTrust")]
public sealed class HtmlElement

The HtmlElement type exposes the following members.

Public propertyAllGets an HtmlElementCollection of all elements underneath the current element.
Public propertyCanHaveChildrenGets a value indicating whether this element can have child elements.
Public propertyChildrenGets an HtmlElementCollection of all children of the current element.
Public propertyClientRectangleGets the bounds of the client area of the element in the HTML document.
Public propertyDocumentGets the HtmlDocument to which this element belongs.
Public propertyDomElementGets an unmanaged interface pointer for this element.
Public propertyEnabledGets or sets whether the user can input data into this element.
Public propertyFirstChildGets the next element below this element in the document tree.
Public propertyIdGets or sets a label by which to identify the element.
Public propertyInnerHtmlGets or sets the HTML markup underneath this element.
Public propertyInnerTextGets or sets the text assigned to the element.
Public propertyNameGets or sets the name of the element.
Public propertyNextSiblingGets the next element at the same level as this element in the document tree.
Public propertyOffsetParentGets the element from which OffsetRectangle is calculated.
Public propertyOffsetRectangleGets the location of an element relative to its parent.
Public propertyOuterHtmlGets or sets the current element's HTML code.
Public propertyOuterTextGets or sets the current element's text.
Public propertyParentGets the current element's parent element.
Public propertyScrollLeftGets or sets the distance between the edge of the element and the left edge of its content.
Public propertyScrollRectangleGets the dimensions of an element's scrollable region.
Public propertyScrollTopGets or sets the distance between the edge of the element and the top edge of its content.
Public propertyStyleGets or sets a comma-delimited list of styles for the current element.
Public propertyTabIndexGets or sets the location of this element in the tab order.
Public propertyTagNameGets the name of the HTML tag.

Public methodAppendChildAdds an element to another element's subtree.
Public methodAttachEventHandlerAdds an event handler for a named event on the HTML Document Object Model (DOM).
Public methodDetachEventHandlerRemoves an event handler from a named event on the HTML Document Object Model (DOM).
Public methodEqualsTests if the supplied object is equal to the current element. (Overrides Object.Equals(Object).)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodFocusPuts user input focus on the current element.
Public methodGetAttributeRetrieves the value of the named attribute on the element.
Public methodGetElementsByTagNameRetrieves a collection of elements represented in HTML by the specified HTML tag.
Public methodGetHashCodeServes as a hash function for a particular type. (Overrides Object.GetHashCode().)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodInsertAdjacentElementInsert a new element into the Document Object Model (DOM).
Public methodInvokeMember(String)Executes an unexposed method on the underlying DOM element of this element.
Public methodInvokeMember(String, Object[])Executes a function defined in the current HTML page by a scripting language.
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodRaiseEventCauses the named event to call all registered event handlers.
Public methodRemoveFocusRemoves focus from the current element, if that element has focus.
Public methodScrollIntoViewScrolls through the document containing this element until the top or bottom edge of this element is aligned with the document's window.
Public methodSetAttributeSets the value of the named attribute on the element.
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)

Public eventClickOccurs when the user clicks on the element with the left mouse button.
Public eventDoubleClickOccurs when the user clicks the left mouse button over an element twice, in rapid succession.
Public eventDragOccurs when the user drags text to various locations.
Public eventDragEndOccurs when a user finishes a drag operation.
Public eventDragLeaveOccurs when the user is no longer dragging an item over this element.
Public eventDragOverOccurs when the user drags text over the element.
Public eventFocusingOccurs when the element first receives user input focus.
Public eventGotFocusOccurs when the element has received user input focus.
Public eventKeyDownOccurs when the user presses a key on the keyboard.
Public eventKeyPressOccurs when the user presses and releases a key on the keyboard.
Public eventKeyUpOccurs when the user releases a key on the keyboard.
Public eventLosingFocusOccurs when the element is losing user input focus.
Public eventLostFocusOccurs when the element has lost user input focus.
Public eventMouseDownOccurs when the user presses a mouse button.
Public eventMouseEnterOccurs when the user first moves the mouse cursor over the current element.
Public eventMouseLeaveOccurs when the user moves the mouse cursor off of the current element.
Public eventMouseMoveOccurs when the user moves the mouse cursor across the element.
Public eventMouseOverOccurs when the mouse cursor enters the bounds of the element.
Public eventMouseUpOccurs when the user releases a mouse button.

Public operatorStatic memberEqualityCompares two elements for equality.
Public operatorStatic memberInequalityCompares two HtmlElement objects for inequality.

HtmlElement represents any possible type of element in an HTML document, such as BODY, TABLE, and FORM, among others. The class exposes the most common properties you can expect to find on all elements.

Most elements can have child elements: other HTML elements that are placed underneath them. Use the CanHaveChildren property to test whether a given element has children, and the Children collection to iterate through them. The Parent property returns the HtmlElement in which the current element is nested.

You often need access to attributes, properties, and methods on the underlying element that are not directly exposed by HtmlElement, such as the SRC attribute on an IMG element or the Submit method on a FORM. The GetAttribute and SetAttribute methods enable you to retrieve and alter any attribute or property on a specific element, while InvokeMember provides access to any methods not exposed in the managed Document Object Model (DOM). If your application has unmanaged code permission, you can also access unexposed properties and methods with the DomElement attribute.

Use the TagName property to test whether an element is of a specific type.

Any HTML document can be modified at run time. You can create new HtmlElement objects with the CreateElement method of HtmlDocument, and add them to another element using the AppendChild or InsertAdjacentElement methods. You can also create the elements as HTML tags and assign them to an existing element's InnerHtml property.

The following code example shows how to examine an arbitrary HTML document and derive a string describing the HTML elements, with indentation and level numbers used to indicate how deeply nested the elements are in the document. This code example requires that your application hosts a WebBrowser control named WebBrowser1.

		private void PrintDomBegin()
			if (webBrowser1.Document != null)
				HtmlElementCollection elemColl = null;
				HtmlDocument doc = webBrowser1.Document;
				if (doc != null)
					elemColl = doc.GetElementsByTagName("HTML");
					String str = PrintDom(elemColl, new System.Text.StringBuilder(), 0);
					webBrowser1.DocumentText = str;

		private string PrintDom(HtmlElementCollection elemColl, System.Text.StringBuilder returnStr, Int32 depth)
			System.Text.StringBuilder str = new System.Text.StringBuilder();

			foreach (HtmlElement elem in elemColl)
				string elemName;

				elemName = elem.GetAttribute("ID");
				if (elemName == null || elemName.Length == 0)
					elemName = elem.GetAttribute("name");
					if (elemName == null || elemName.Length == 0)
						elemName = "<no name>";

				str.Append(' ', depth * 4);
				str.Append(elemName + ": " + elem.TagName + "(Level " + depth + ")");

				if (elem.CanHaveChildren)
					PrintDom(elem.Children, returnStr, depth + 1);

				str.Remove(0, str.Length);


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.