Click here to Skip to main content
15,896,207 members
Articles / Programming Languages / Visual Basic

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

Rate me:
Please Sign up or sign in to vote.
2.58/5 (8 votes)
29 May 20051 min read 55K   128   29  
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()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        '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
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    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.SuspendLayout()
        '
        'BtnRed
        '
        Me.BtnRed.Location = New System.Drawing.Point(32, 16)
        Me.BtnRed.Name = "BtnRed"
        Me.BtnRed.TabIndex = 0
        Me.BtnRed.Text = "Red"
        '
        'BtnWhite
        '
        Me.BtnWhite.Location = New System.Drawing.Point(32, 40)
        Me.BtnWhite.Name = "BtnWhite"
        Me.BtnWhite.TabIndex = 1
        Me.BtnWhite.Text = "White"
        '
        'BtnBlue
        '
        Me.BtnBlue.Location = New System.Drawing.Point(32, 64)
        Me.BtnBlue.Name = "BtnBlue"
        Me.BtnBlue.TabIndex = 2
        Me.BtnBlue.Text = "Blue"
        '
        'frmChild
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(200, 110)
        Me.Controls.Add(Me.BtnBlue)
        Me.Controls.Add(Me.BtnWhite)
        Me.Controls.Add(Me.BtnRed)
        Me.Name = "frmChild"
        Me.Text = "frmChild"
        Me.ResumeLayout(False)

    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
        'ChangeColor()
    End Sub
    Private Sub BtnWhite_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnWhite.Click
        iColorObserver.Color = ColorObserver.Colors.White
        'ChangeColor()
    End Sub
    Private Sub BtnBlue_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnBlue.Click
        iColorObserver.Color = ColorObserver.Colors.Blue
        'ChangeColor()
    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.

License

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


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

Comments and Discussions