Click here to Skip to main content
13,299,436 members (62,048 online)
Click here to Skip to main content
Add your own
alternative version


30 bookmarked
Posted 5 Jan 2004

Implementing Bubble Events in VB.NET Windows Forms (Chain of Responsibility Pattern)

Rate this:
Please Sign up or sign in to vote.
Here you will know how to implement a pattern which will allow you to handle events on different levels of the object hierarchy.

What is a Bubble event?

Sometimes you need to handle one event on different layers of the class hierarchy. Take for example, the following case:

You need to implement a system which will receive data from the serial connection, parse it to single out several commands and handle them accordingly. The application will have to report its activity to a form. The form must present:

  1. a list of several connections
  2. the number of bytes sent and received for each connection
  3. last executed command for each connection

One of the possible approaches is:

  1. create a base class for low-level serial communication (Connection)
  2. create a class that will handle the commands and send the response back to the caller (Communicator)
  3. create a class that will contain several Communicators (CommManager)

You need to handle the following events:

  1. OnData

    It is fired when data is sent or received to/from the serial port.

  2. OnCommand

    It is fired when a command has been handled by the Communicator.

  3. OnError

    If any errors occur in the data transmission, this event is fired by Connection.

The OnData event is defined in Connection class. It is handled in Communicator to parse the input buffer in order to determine which command to run. Also it has to be propagated to the CommManager class, which will in turn notify its parent form that some data has been processed.

The OnError event is defined in Connection class too. It will notify parent classes about errors in the serial communication.

The OnCommand event is defined in Communicator class. It is fired when a command has been handled. It will notify the form about the last executed command.

I defined the BubbleEvent as an event that is to be handled or propagated through several objects, either containing or aggregating others. The following implementation follows the “Chain of Responsibility” pattern.


In order to implement the scenario, we need the following things:

This is the interface that event handlers will have to implement:

Public Interface IBubbleEventHandler
    Property EventSuccessor() As IBubbleEventHandler
    Function HandleBubbleEvent(ByRef evt As BubbleEvent)
    Function ForwardBubbleEvent(ByRef evt As BubbleEvent)
End Interface

EventSuccessor is a property that sets or returns the next handler in the chain. HandleBubbleEvent is a method that either handles or calls ForwardBubbleEvent which forwards the event to the next handler in the chain.

' BubbleEvent
  ' This is the base class for BubbleEvents
  ' It provdes basic event properties:
  ' Name – event ID for reference purposes
  ' Parameters collection – event parameters
  Public MustInherit Class BubbleEvent
    Private _name As String
    Private _params As ArrayList
    Public ReadOnly Property Name() As String
        Return _name
      End Get
    End Property
    Public ReadOnly Property Parameters() As ArrayList
        Return _params
      End Get
    End Property
    Public Sub New(ByVal astrEvtName As String, _ 
         ByRef aobjEvtSource As IBubbleEventHandler, _ 
         ByVal ParamArray params() As Object)
      Dim p
      _name = astrEvtName
      _params = New ArrayList()
      For Each p In params
    End Sub
  End Class

This is what we will implement in the sample application.

The Sample

We well implement the above case in terms of abstract objects. See the demonstration project for details.

In the sample we have a base class, a child class inherited from the base class, and a child class collection. The collection will be represented by a ListView control on a form. Whenever you select an entry in the ListView and change Prop1 property of the selected item, you’ll see message boxes showing you the levels at which the Change event is handled.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Serge Lobko-Lobanovsky
Web Developer
Belarus Belarus
I work for Logic Software ( as a lead .NET developer/architect.

You may also be interested in...

Comments and Discussions

GeneralThis example does not work Pin
Member 3081796-Sep-08 4:48
memberMember 3081796-Sep-08 4:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171207.1 | Last Updated 6 Jan 2004
Article Copyright 2004 by Serge Lobko-Lobanovsky
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid