We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: November 16, 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

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

Platform: WinForms |
Control: HTMLUIControl |
Published Date: April 30, 2011 |
Last Revised Date: January 3, 2020

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

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon