Click here to Skip to main content
13,052,696 members (64,875 online)
Click here to Skip to main content


29 bookmarked
Posted 29 May 2005

Using an Observer design pattern to keep multiple forms up to date

, 29 May 2005
This article demonstrates how multiple forms can be updated simultaneously using Singleton and Observer Design Patterns.
Public Class frmChild
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()

        'This call is required by the Windows Form Designer.

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
            End If
        End If
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents BtnRed As System.Windows.Forms.Button
    Friend WithEvents BtnWhite As System.Windows.Forms.Button
    Friend WithEvents BtnBlue As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.BtnRed = New System.Windows.Forms.Button
        Me.BtnWhite = New System.Windows.Forms.Button
        Me.BtnBlue = New System.Windows.Forms.Button
        Me.BtnRed.Location = New System.Drawing.Point(32, 16)
        Me.BtnRed.Name = "BtnRed"
        Me.BtnRed.TabIndex = 0
        Me.BtnRed.Text = "Red"
        Me.BtnWhite.Location = New System.Drawing.Point(32, 40)
        Me.BtnWhite.Name = "BtnWhite"
        Me.BtnWhite.TabIndex = 1
        Me.BtnWhite.Text = "White"
        Me.BtnBlue.Location = New System.Drawing.Point(32, 64)
        Me.BtnBlue.Name = "BtnBlue"
        Me.BtnBlue.TabIndex = 2
        Me.BtnBlue.Text = "Blue"
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(200, 110)
        Me.Name = "frmChild"
        Me.Text = "frmChild"

    End Sub

#End Region

    Private iColorObserver As ColorObserver

    Private Sub frmChild_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        iColorObserver = iColorObserver.getSingletonObject
        AddHandler iColorObserver.ColorUpdated, AddressOf ChangeColor
    End Sub

    Private Sub ChangeColor()
        Select Case iColorObserver.Color
            Case ColorObserver.Colors.Red
                Me.BackColor = Color.Red
            Case ColorObserver.Colors.White
                Me.BackColor = Color.White
            Case ColorObserver.Colors.Blue
                Me.BackColor = Color.Blue
        End Select
    End Sub

    Private Sub BtnRed_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnRed.Click
        iColorObserver.Color = ColorObserver.Colors.Red
    End Sub
    Private Sub BtnWhite_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnWhite.Click
        iColorObserver.Color = ColorObserver.Colors.White
    End Sub
    Private Sub BtnBlue_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnBlue.Click
        iColorObserver.Color = ColorObserver.Colors.Blue
    End Sub
End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


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

Software Developer (Senior)
Switzerland Switzerland
I am a Software Engineer currently working in a Re Insurance company.

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170713.1 | Last Updated 30 May 2005
Article Copyright 2005 by Nigel-Findlater
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid