Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- namespace Custom_Events_Test
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- Person p1 = new Person();
- Person p2 = new Person();
- private void Form1_Load(object sender, EventArgs e)
- {
- CreateEvents();
- p1.Name = "Jesse";
- p2.Name = "Brian";
- txtName1.Text = p1.Name;
- txtName2.Text = p2.Name;
- }
- private void CreateEvents()
- {
- p1.OnCashChanged += new Person.CashEventHandler(person_OnCashChanged);
- p1.OnCashAdded += new Person.CashEventHandler(person_OnCashAdded);
- p1.OnCashRemoved += new Person.CashEventHandler(person_OnCashRemoved);
- p2.OnCashChanged += new Person.CashEventHandler(person_OnCashChanged);
- p2.OnCashAdded += new Person.CashEventHandler(person_OnCashAdded);
- p2.OnCashRemoved += new Person.CashEventHandler(person_OnCashRemoved);
- }
- void person_OnCashRemoved(Person x)
- {
- txtLogs.Text += x.Name + " spent Money!" + "\r\n";
- }
- void person_OnCashAdded(Person x)
- {
- txtLogs.Text += x.Name+" got "+x.GetCash()+" cash!" + "\r\n";
- }
- void person_OnCashChanged(Person x)
- {
- lblCash.Text = x.GetCash().ToString() + "$";
- lblCash2.Text = x.GetCash().ToString() + "$";
- //txtLogs.Text += x.Name+"'s Cash has been changed!" +"\r\n";
- }
- private void buttonExit_Click(object sender, EventArgs e)
- {
- Application.Exit();
- }
- private void buttonAddCash_Click(object sender, EventArgs e)
- {
- p1.AddCash(p1, double.Parse(txtAddCash.Text));
- }
- private void buttonSetCash_Click(object sender, EventArgs e)
- {
- p1.SetCash(p1, double.Parse(txtAddCash.Text));
- }
- private void buttonRemoveCash_Click(object sender, EventArgs e)
- {
- p1.RemoveCash(p1, double.Parse(txtRemoveCash.Text));
- }
- private void button1_Click(object sender, EventArgs e)
- {
- MessageBox.Show("" + p1.Name + " has " + p1.GetCash().ToString() + "$");
- }
- private void buttonAddCash2_Click(object sender, EventArgs e)
- {
- p2.AddCash(p2, double.Parse(txt1.Text));
- }
- private void buttonSetCash2_Click(object sender, EventArgs e)
- {
- p2.SetCash(p2, double.Parse(txt2.Text));
- }
- private void buttonRemoveCash2_Click(object sender, EventArgs e)
- {
- p2.RemoveCash(p2, double.Parse(txt3.Text));
- }
- }
- class Person
- {
- public delegate void CashEventHandler(Person x);
- public event CashEventHandler OnCashChanged;
- public event CashEventHandler OnCashAdded;
- public event CashEventHandler OnCashRemoved;
- private double _Cash;
- private string _Name;
- public string Name
- {
- get { return _Name; }
- set { _Name = value; }
- }
- public void SetCash(Person p, double Amount)
- {
- if (Amount != _Cash && OnCashChanged != null) OnCashChanged(p);
- this._Cash = Amount;
- }
- public void AddCash(Person p, double Amount)
- {
- this._Cash += Amount;
- if (OnCashChanged != null) OnCashChanged(p);
- if (OnCashAdded != null) OnCashAdded(p);
- }
- public void RemoveCash(Person p, double Amount)
- {
- if (Amount <= 0) return;
- this._Cash -= Amount;
- if (OnCashChanged != null) OnCashChanged(p);
- if (OnCashRemoved != null) OnCashRemoved(p);
- }
- public double GetCash()
- {
- return this._Cash;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement