HtmlElement Class
TOC
Collapse the table of content
Expand the table of content

HtmlElement Class

 

Represents an HTML element inside of a Web page.

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

System.Object
  System.Windows.Forms.HtmlElement

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

NameDescription
System_CAPS_pubpropertyAll

Gets an HtmlElementCollection of all elements underneath the current element.

System_CAPS_pubpropertyCanHaveChildren

Gets a value indicating whether this element can have child elements.

System_CAPS_pubpropertyChildren

Gets an HtmlElementCollection of all children of the current element.

System_CAPS_pubpropertyClientRectangle

Gets the bounds of the client area of the element in the HTML document.

System_CAPS_pubpropertyDocument

Gets the HtmlDocument to which this element belongs.

System_CAPS_pubpropertyDomElement

Gets an unmanaged interface pointer for this element.

System_CAPS_pubpropertyEnabled

Gets or sets whether the user can input data into this element.

System_CAPS_pubpropertyFirstChild

Gets the next element below this element in the document tree.

System_CAPS_pubpropertyId

Gets or sets a label by which to identify the element.

System_CAPS_pubpropertyInnerHtml

Gets or sets the HTML markup underneath this element.

System_CAPS_pubpropertyInnerText

Gets or sets the text assigned to the element.

System_CAPS_pubpropertyName

Gets or sets the name of the element.

System_CAPS_pubpropertyNextSibling

Gets the next element at the same level as this element in the document tree.

System_CAPS_pubpropertyOffsetParent

Gets the element from which OffsetRectangle is calculated.

System_CAPS_pubpropertyOffsetRectangle

Gets the location of an element relative to its parent.

System_CAPS_pubpropertyOuterHtml

Gets or sets the current element's HTML code.

System_CAPS_pubpropertyOuterText

Gets or sets the current element's text.

System_CAPS_pubpropertyParent

Gets the current element's parent element.

System_CAPS_pubpropertyScrollLeft

Gets or sets the distance between the edge of the element and the left edge of its content.

System_CAPS_pubpropertyScrollRectangle

Gets the dimensions of an element's scrollable region.

System_CAPS_pubpropertyScrollTop

Gets or sets the distance between the edge of the element and the top edge of its content.

System_CAPS_pubpropertyStyle

Gets or sets a semicolon-delimited list of styles for the current element.

System_CAPS_pubpropertyTabIndex

Gets or sets the location of this element in the tab order.

System_CAPS_pubpropertyTagName

Gets the name of the HTML tag.

NameDescription
System_CAPS_pubmethodAppendChild(HtmlElement)

Adds an element to another element's subtree.

System_CAPS_pubmethodAttachEventHandler(String, EventHandler)

Adds an event handler for a named event on the HTML Document Object Model (DOM).

System_CAPS_pubmethodDetachEventHandler(String, EventHandler)

Removes an event handler from a named event on the HTML Document Object Model (DOM).

System_CAPS_pubmethodEquals(Object)

Tests if the supplied object is equal to the current element.(Overrides Object.Equals(Object).)

System_CAPS_pubmethodFocus()

Puts user input focus on the current element.

System_CAPS_pubmethodGetAttribute(String)

Retrieves the value of the named attribute on the element.

System_CAPS_pubmethodGetElementsByTagName(String)

Retrieves a collection of elements represented in HTML by the specified HTML tag.

System_CAPS_pubmethodGetHashCode()

Serves as a hash function for a particular type.(Overrides Object.GetHashCode().)

System_CAPS_pubmethodGetType()

Gets the Type of the current instance.(Inherited from Object.)

System_CAPS_pubmethodInsertAdjacentElement(HtmlElementInsertionOrientation, HtmlElement)

Insert a new element into the Document Object Model (DOM).

System_CAPS_pubmethodInvokeMember(String)

Executes an unexposed method on the underlying DOM element of this element.

System_CAPS_pubmethodInvokeMember(String, Object[])

Executes a function defined in the current HTML page by a scripting language.

System_CAPS_pubmethodRaiseEvent(String)

Causes the named event to call all registered event handlers.

System_CAPS_pubmethodRemoveFocus()

Removes focus from the current element, if that element has focus.

System_CAPS_pubmethodScrollIntoView(Boolean)

Scrolls through the document containing this element until the top or bottom edge of this element is aligned with the document's window.

System_CAPS_pubmethodSetAttribute(String, String)

Sets the value of the named attribute on the element.

System_CAPS_pubmethodToString()

Returns a string that represents the current object.(Inherited from Object.)

NameDescription
System_CAPS_pubeventClick

Occurs when the user clicks on the element with the left mouse button.

System_CAPS_pubeventDoubleClick

Occurs when the user clicks the left mouse button over an element twice, in rapid succession.

System_CAPS_pubeventDrag

Occurs when the user drags text to various locations.

System_CAPS_pubeventDragEnd

Occurs when a user finishes a drag operation.

System_CAPS_pubeventDragLeave

Occurs when the user is no longer dragging an item over this element.

System_CAPS_pubeventDragOver

Occurs when the user drags text over the element.

System_CAPS_pubeventFocusing

Occurs when the element first receives user input focus.

System_CAPS_pubeventGotFocus

Occurs when the element has received user input focus.

System_CAPS_pubeventKeyDown

Occurs when the user presses a key on the keyboard.

System_CAPS_pubeventKeyPress

Occurs when the user presses and releases a key on the keyboard.

System_CAPS_pubeventKeyUp

Occurs when the user releases a key on the keyboard.

System_CAPS_pubeventLosingFocus

Occurs when the element is losing user input focus.

System_CAPS_pubeventLostFocus

Occurs when the element has lost user input focus.

System_CAPS_pubeventMouseDown

Occurs when the user presses a mouse button.

System_CAPS_pubeventMouseEnter

Occurs when the user first moves the mouse cursor over the current element.

System_CAPS_pubeventMouseLeave

Occurs when the user moves the mouse cursor off of the current element.

System_CAPS_pubeventMouseMove

Occurs when the user moves the mouse cursor across the element.

System_CAPS_pubeventMouseOver

Occurs when the mouse cursor enters the bounds of the element.

System_CAPS_pubeventMouseUp

Occurs when the user releases a mouse button.

NameDescription
System_CAPS_puboperatorSystem_CAPS_staticEquality(HtmlElement, HtmlElement)

Compares two elements for equality.

System_CAPS_puboperatorSystem_CAPS_staticInequality(HtmlElement, HtmlElement)

Compares 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 + ")");
		returnStr.AppendLine(str.ToString());

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

		str.Remove(0, str.Length);
	}

	return(returnStr.ToString());
}

.NET Framework
Available since 2.0

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

Return to top
Show:
© 2016 Microsoft