Sunday, February 8, 2015
Adding Events To A Class
//=====WaterTankEventArgs class in EventProducerLib============
namespace EventProducerLib
{
public class WaterTankEventArgs
{
//contains data to send back to the event handler
private double _amount;
private string _message;
public WaterTankEventArgs(double amount, string message)
{
_amount = amount;
_message = message;
}
public double Amount { get { return this._amount; } }
public string Message { get { return this._message; } }
}
}
//==================================================
namespace EventProducerLib
{
//1. Define a delegate type
public delegate void WaterTankHandler(object sender, WaterTankEventArgs e);
public class WaterTank
{
private double _capacity; // in gallons
private double _currentAmount; // in gallons
//this is the producer class
//it produces (generates) an event
//An event provides a callback mechanism
//callback means event producer(class) calls back to window apps
//producer doesn't know which method to call, that is why it uses delegate to call methods.
//delegate is reference to methods
//2. define an event that is raised when WaterTank overflows
public event WaterTankHandler TankOverflow;
public event WaterTankHandler TankEmpty;
public WaterTank(double capacity, double initialAmount)
{
_capacity = capacity;
_currentAmount = initialAmount;
}
public double Capacity
{ get { return _capacity; } }
public double CurrentAmount
{ get { return _currentAmount; } }
public void AddWater(double amount)
{
if (_currentAmount + amount > _capacity)
{
//notify the user of this class thru an event
//3. add code to raise / fire the Tankoverflow
if (TankOverflow != null)
{
WaterTankEventArgs e = new WaterTankEventArgs(_currentAmount, "Tank will exceed capacity, Re-enter new amount");
TankOverflow(this, e);
}
}
else
_currentAmount += amount;
}
public void RemoveWater(double amount)
{
if (_currentAmount - amount < 0)
{
//notify user, thru an event
if (TankEmpty != null) //**********************************
{
WaterTankEventArgs et = new WaterTankEventArgs(_currentAmount, "You do not have enough water to remove, Re-enter new amount");
TankEmpty(this, et);
}
}
else
_currentAmount -= amount;
}
}
}
//=====================================================
using System.Windows.Forms;
using EventProducerLib; //Added to work class library
namespace AddingEventsToAClass
{
//Use a class that defines an event
//have the class fire an event
//have this form consume the event => listening to an event
//means providing a method (event handler) that gets called when the event occurs
//(meaning when the event is fired)
public partial class Form1 : Form
{
WaterTank wt = null;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
groupBox2.Enabled = false;
}
//TankOverFlow method handler
private void wt_TankOverflow(object sender, WaterTankEventArgs e)
{
MessageBox.Show("Current amount: " + e.Amount + "\n" + e.Message);
}
//TankEmpty method handler
private void wt_TankEmpty(object sender, WaterTankEventArgs et)
{
MessageBox.Show("Current amount: " + et.Amount + "\n" + et.Message);
}
private void btnCreateWaterTank_Click(object sender, EventArgs e)
{
try
{
double capacity = double.Parse(txtCapacity.Text);
double initialAmt = double.Parse(txtInitialAmount.Text);
if (initialAmt <= capacity)
{
//Create water tank
wt = new WaterTank(capacity, initialAmt);
//attach the TankOverFlow event handler
//register to listen to the TankOverFlow event
//1. Create a delegate object from the delegate type defined in the WaterTank class
//old fashion way
//WaterTankHandler wth = wt_TankOverflow;
//wt.TankOverflow += wth;
//this is a shortcut to link method with delegate
wt.TankOverflow += wt_TankOverflow;
wt.TankEmpty += wt_TankEmpty;
//disable create, enable other functions
groupBox1.Enabled = false;
groupBox2.Enabled = true;
}
else
{
MessageBox.Show("Initial gallons amount cannot exceed capacity");
txtCapacity.Clear();
txtInitialAmount.Clear();
}
}
catch (FormatException fe)
{
MessageBox.Show(fe.Message);
}
}
private void btnAddWater_Click(object sender, EventArgs e)
{
try
{
double amount = double.Parse(txtWaterGtoAorR.Text);
//add amount to the tank wt
wt.AddWater(amount);
lblCurrentLevel.Text = "Current Amount = " + wt.CurrentAmount;
}
catch (FormatException fe)
{
MessageBox.Show(fe.Message);
}
}
private void btnRemoveWater_Click(object sender, EventArgs e)
{
try
{
double amount = double.Parse(txtWaterGtoAorR.Text);
//add amount to the tank wt
wt.RemoveWater(amount);
lblCurrentLevel.Text = "Current Amount = " + wt.CurrentAmount;
}
catch (FormatException fe)
{
MessageBox.Show(fe.Message);
}
}
}
}
///Assignment:
///Define a second event TankEmpty (below zero)
///Fire it in the remove water method
///Register this event in the Form
///Demonstrate that it works
Labels:
C#Advance
Subscribe to:
Post Comments (Atom)


No comments:
Post a Comment