Export (0) Print
Expand All

HtmlElement Class

Represents an HTML element inside of a Web page.

System.Object
  System.Windows.Forms.HtmlElement

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

'Declaration
<PermissionSetAttribute(SecurityAction.LinkDemand, Name := "FullTrust")> _
Public NotInheritable Class HtmlElement

The HtmlElement type exposes the following members.

  NameDescription
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.
Top

  NameDescription
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).)
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 the default hash function. (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.
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.)
Top

  NameDescription
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.
Top

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

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 Sub PrintDomBegin()
    If (WebBrowser1.Document IsNot Nothing) Then 
        Dim ElemColl As HtmlElementCollection

        Dim Doc As HtmlDocument = WebBrowser1.Document
        If (Not (Doc Is Nothing)) Then
            ElemColl = Doc.GetElementsByTagName("HTML")
            Dim Str As String = PrintDom(ElemColl, New System.Text.StringBuilder(), 0)

            WebBrowser1.DocumentText = Str
        End If 
    End If 
End Sub 

Private Function PrintDom(ByVal ElemColl As HtmlElementCollection, ByRef ReturnStr As System.Text.StringBuilder, ByVal Depth As Integer) As String 
    Dim Str As New System.Text.StringBuilder()

    For Each Elem As HtmlElement In ElemColl
        Dim ElemName As String

        ElemName = Elem.GetAttribute("ID")
        If (ElemName Is Nothing Or ElemName.Length = 0) Then
            ElemName = Elem.GetAttribute("name")
            If (ElemName Is Nothing Or ElemName.Length = 0) Then
                ElemName = "<no name>" 
            End If 
        End If

        Str.Append(CChar(" "), Depth * 4)
        Str.Append(ElemName & ": " & Elem.TagName & "(Level " & Depth & ")")
        ReturnStr.AppendLine(Str.ToString())

        If (Elem.CanHaveChildren) Then
            PrintDom(Elem.Children, ReturnStr, Depth + 1)
        End If

        Str.Remove(0, Str.Length)
    Next

    PrintDom = ReturnStr.ToString()
End Function

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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.
Show:
© 2014 Microsoft