Advertisement
Srgjan-LDTeam

C# Custom Events Example

Jan 16th, 2015
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.13 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9.  
  10. namespace Custom_Events_Test
  11. {
  12.     public partial class Form1 : Form
  13.     {
  14.         public Form1()
  15.         {
  16.             InitializeComponent();
  17.         }
  18.  
  19.         Person p1 = new Person();
  20.         Person p2 = new Person();
  21.  
  22.         private void Form1_Load(object sender, EventArgs e)
  23.         {
  24.             CreateEvents();
  25.             p1.Name = "Jesse";
  26.             p2.Name = "Brian";
  27.             txtName1.Text = p1.Name;
  28.             txtName2.Text = p2.Name;
  29.         }
  30.  
  31.         private void CreateEvents()
  32.         {
  33.             p1.OnCashChanged += new Person.CashEventHandler(person_OnCashChanged);
  34.             p1.OnCashAdded += new Person.CashEventHandler(person_OnCashAdded);
  35.             p1.OnCashRemoved += new Person.CashEventHandler(person_OnCashRemoved);
  36.             p2.OnCashChanged += new Person.CashEventHandler(person_OnCashChanged);
  37.             p2.OnCashAdded += new Person.CashEventHandler(person_OnCashAdded);
  38.             p2.OnCashRemoved += new Person.CashEventHandler(person_OnCashRemoved);
  39.         }
  40.  
  41.         void person_OnCashRemoved(Person x)
  42.         {
  43.             txtLogs.Text += x.Name + " spent Money!" + "\r\n";
  44.         }
  45.  
  46.         void person_OnCashAdded(Person x)
  47.         {
  48.             txtLogs.Text += x.Name+" got "+x.GetCash()+" cash!" + "\r\n";
  49.         }
  50.  
  51.         void person_OnCashChanged(Person x)
  52.         {
  53.             lblCash.Text = x.GetCash().ToString() + "$";
  54.             lblCash2.Text = x.GetCash().ToString() + "$";
  55.             //txtLogs.Text += x.Name+"'s Cash has been changed!" +"\r\n";
  56.         }
  57.  
  58.         private void buttonExit_Click(object sender, EventArgs e)
  59.         {
  60.             Application.Exit();
  61.         }
  62.  
  63.         private void buttonAddCash_Click(object sender, EventArgs e)
  64.         {
  65.             p1.AddCash(p1, double.Parse(txtAddCash.Text));
  66.         }
  67.  
  68.         private void buttonSetCash_Click(object sender, EventArgs e)
  69.         {
  70.             p1.SetCash(p1, double.Parse(txtAddCash.Text));
  71.         }
  72.  
  73.         private void buttonRemoveCash_Click(object sender, EventArgs e)
  74.         {
  75.             p1.RemoveCash(p1, double.Parse(txtRemoveCash.Text));
  76.         }
  77.  
  78.         private void button1_Click(object sender, EventArgs e)
  79.         {
  80.             MessageBox.Show("" + p1.Name + " has " + p1.GetCash().ToString() + "$");
  81.         }
  82.  
  83.         private void buttonAddCash2_Click(object sender, EventArgs e)
  84.         {
  85.             p2.AddCash(p2, double.Parse(txt1.Text));
  86.         }
  87.  
  88.         private void buttonSetCash2_Click(object sender, EventArgs e)
  89.         {
  90.             p2.SetCash(p2, double.Parse(txt2.Text));
  91.         }
  92.  
  93.         private void buttonRemoveCash2_Click(object sender, EventArgs e)
  94.         {
  95.             p2.RemoveCash(p2, double.Parse(txt3.Text));
  96.         }
  97.     }
  98.  
  99.     class Person
  100.     {
  101.         public delegate void CashEventHandler(Person x);
  102.  
  103.         public event CashEventHandler OnCashChanged;
  104.         public event CashEventHandler OnCashAdded;
  105.         public event CashEventHandler OnCashRemoved;
  106.  
  107.         private double _Cash;
  108.         private string _Name;
  109.  
  110.         public string Name
  111.         {
  112.             get { return _Name; }
  113.             set { _Name = value; }
  114.         }
  115.  
  116.         public void SetCash(Person p, double Amount)
  117.         {
  118.             if (Amount != _Cash && OnCashChanged != null) OnCashChanged(p);
  119.             this._Cash = Amount;
  120.         }
  121.  
  122.         public void AddCash(Person p, double Amount)
  123.         {
  124.             this._Cash += Amount;
  125.             if (OnCashChanged != null) OnCashChanged(p);
  126.             if (OnCashAdded != null) OnCashAdded(p);
  127.         }
  128.  
  129.         public void RemoveCash(Person p, double Amount)
  130.         {
  131.             if (Amount <= 0) return;
  132.             this._Cash -= Amount;
  133.             if (OnCashChanged != null) OnCashChanged(p);
  134.             if (OnCashRemoved != null) OnCashRemoved(p);
  135.         }
  136.        
  137.         public double GetCash()
  138.         {
  139.             return this._Cash;
  140.         }
  141.     }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement