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 DELEGATE_EVENT
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//first create class
public class Amount
{
int amount=0;
//create delegate
public delegate void bankdelegate(int oldbal, int newbal);
//create two events
public event bankdelegate OnDeposit;
public event bankdelegate OnWithDraw;
//create two methods
public void Deposit(int a)
{
OnDeposit(this.amount, this.amount + a);// call event
this.amount += a;
}
public void WithDraw(int a)
{
OnWithDraw(this.amount, this.amount - a);
this.amount -= a;
}
}
//create ref of class
public Amount amount=null;
private void btndeposit_Click(object sender, EventArgs e)
{
amount.Deposit(Convert.ToInt32(txtamount.Text));
}
private void Form1_Load(object sender, EventArgs e)
{
amount=new Amount();
//load event On form Load
amount.OnDeposit +=new Amount.bankdelegate(amount_OnDeposit);
amount.OnWithDraw +=new Amount.bankdelegate(amount_OnWithDraw);
}
//create function
public void amount_OnDeposit(int oldbal, int newbal)
{
lbloldvalance.Text = oldbal.ToString();
lblnewbalance.Text = newbal.ToString();
}
public void amount_OnWithDraw(int oldbal, int newbal)
{
lbloldvalance.Text = oldbal.ToString();
lblnewbalance.Text = newbal.ToString();
}
private void btnwithdraw_Click(object sender, EventArgs e)
{
amount.WithDraw(Convert.ToInt32(txtamount.Text));
}
}
}
Comments
Post a Comment