Category / Section
What is bubbling event architecture and construct it in WinForms HTMLUIControl?
2 mins read
Bubbling event
The use of a common 'Event Handler' for handling the same event for all the child elements is called Bubbling Event Architecture. This helps in reducing the number of event handler declarations required by each and every child element, thereby reducing the length and complexity of the code.
The HTML elements are collected in an array by their parent name. An event handler, for a specific event, common to all the elements in the collection is defined. The bubbling event arguments for the elements are set. The element that first triggers the event is traced. Based on the element's identity, the action for the event is executed.
C#
//The elements within the BODY tag(parent element) are got in an array IHTMLElement[] htmlelement = this.htmluiControl1.Document.GetElementsByName("body"); //A common event handler is defined for all the elements in the collection htmlelement[0].Click += new EventHandler(body_Click); //The bubbling event argument for the tag elements are set BubblingEventArgs bargs = HTMLUIControl.GetBublingEventArgs(e); //The element that first triggers the event is obtained BaseElement elem = bargs.RootSender as BaseElement; //The Element is verified for its identity and the event is executed private void body_Click(object sender, EventArgs e) { if(elem.ID == "btn1") { Console.WriteLine("Button1 is clicked"); } else if(elem.ID == "btn2") { Console.WriteLine("Button2 is clicked"); } }
VB
'The elements within the BODY tag(parent element) are got in an array Private htmlelement As IHTMLElement() = Me.htmluiControl1.Document.GetElementsByName("body") 'A common event handler is defined for all the elements in the collection Private htmlelement(0).Click += New EventHandler(body_Click) 'The bubbling event argument for the tag elements are set Private bargs As BubblingEventArgs = HTMLUIControl.GetBublingEventArgs(e) 'The element that first triggers the event is obtained Private elem As BaseElement = CType(IIf(TypeOf bargs.RootSender Is BaseElement, bargs.RootSender, Nothing), BaseElement) 'The Element is verified for its identity and the event is executed Private Sub body_Click(ByVal sender As Object, ByVal e As EventArgs) If elem.ID = "btn1" Then Console.WriteLine("Button1 is clicked") Else If elem.ID = "btn2" Then Console.WriteLine("Button2 is clicked") End If End Sub