Articles in this section
Category / Section

How to access all the child elements of an HTML element in WinForms HTMLUIControl?

2 mins read

IHTML element

The IHTMLElement.Children property of any IHTMLElement collects all the child elements of a specified HTML element inside an IHTMLElementsCollection. The user can access the elements needed for his conditions from this collection.

The following code snippet shows how the child elements of the body element of the given HTML document are searched to access elements containing the 'OnClick' attribute and attaching a Click event to those elements.

C#

private void htmluiControl1_LoadFinished(object sender, System.EventArgs e)
{
     //Getting the body element in the HTML document
     IHTMLElement[] body = this.htmluiControl1.Document.GetElementsByName("body");
     //Collecting the children of the body element in a collection
     IHTMLElementsCollection elem = body[0].Children;
     foreach(IHTMLElement child in elem)
     {
         //searching for the children containing the OnClick attribute
         if(child.Attributes.Contains("ONCLICK") == true)
         {
             //Click event declaration for current children
             child.Click += new EventHandler(child_Click);
         }
     }
}
private void child_Click(object sender, EventArgs e)
{
     BubblingEventArgs bargs = HTMLUIControl.GetBublingEventArgs(e);
     //Accessing the element that is sending the event
     BaseElement elem = bargs.RootSender as BaseElement;
     //Validating the element for execution
     if( elem.ID == "img1" && elem is IMGElementImpl )
     {
         if(elem.Attributes["src"].Value == "sync.jpg")
             elem.Attributes["src"].Value = "syncfusion.gif";
         else
             elem.Attributes["src"].Value = "sync.jpg";
         this.htmluiControl1.ScrollToElement(elem);
     }
}

 

VB

Private Sub htmluiControl1_LoadFinished(ByVal sender As Object, ByVal e As System.EventArgs)
     'Getting the body element in the HTML document
     Dim body As IHTMLElement() = Me.htmluiControl1.Document.GetElementsByName("body")
     'Collecting the children of the body element in a collection
     Dim elem As IHTMLElementsCollection = body(0).Children
     For Each child As IHTMLElement In elem
        'searching for the children containing the OnClick attribute
        If child.Attributes.Contains("ONCLICK") = True Then
           'Click event declaration for current children
           AddHandler child.Click, AddressOf child_Click
        End If
      Next child
End Sub
Private Sub child_Click(ByVal sender As Object, ByVal e As EventArgs)
      Dim bargs As BubblingEventArgs = HTMLUIControl.GetBublingEventArgs(e)
      'Accessing the element that is sending the event
      Dim elem As BaseElement = CType(IIf(TypeOf bargs.RootSender Is BaseElement, bargs.RootSender, Nothing), BaseElement)
      'Validating the element for execution
      If elem.ID = "img1" AndAlso TypeOf elem Is IMGElementImpl Then
         If elem.Attributes("src").Value = "sync.jpg" Then
            elem.Attributes("src").Value = "syncfusion.gif"
         Else
            elem.Attributes("src").Value = "sync.jpg"
         End If
         Me.htmluiControl1.ScrollToElement(elem)
      End If
End Sub

 

Reference link: https://help.syncfusion.com/windowsforms/html-viewer/element-events

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied