Click here to Skip to main content
15,884,814 members
Articles / Desktop Programming / Win32

Implement Observer Pattern in an Absolutely Easy Example

Rate me:
Please Sign up or sign in to vote.
2.84/5 (21 votes)
16 Sep 2008CPOL3 min read 73.5K   1.8K   31  
Observer pattern in C# using Event and Delegate
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace EventAndDelegate
{
    public partial class Form2 : Form
    {
        private Subject objSub;
        Form3 frm;

        public Form2()
        {
            InitializeComponent();
            objSub = new Subject();
        }

        private void Form2_Load(object sender, EventArgs e)
        {

        }

        private void btnOpen_Click(object sender, EventArgs e)
        {
            frm = new Form3(objSub);
            frm.Show();
        }

        private void rdRed_CheckedChanged(object sender, EventArgs e)
        {
            if (rdRed.Checked)
            {
                objSub.UpdateClient("RED");
                lblText.ForeColor = Color.Red;                
            }
        }

        private void rdGreen_CheckedChanged(object sender, EventArgs e)
        {
            if (rdGreen.Checked)
            {
                objSub.UpdateClient("GREEN");
                lblText.ForeColor = Color.Green;
            }
        }

        private void rdBlue_CheckedChanged(object sender, EventArgs e)
        {
            if (rdBlue.Checked)
            {
                objSub.UpdateClient("BLUE");
                lblText.ForeColor = Color.Blue;
            }
        }
    }
}

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Vietnam Vietnam
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions