hi,
I am trying to a write a code for a numberpad which is useful for both Calculator and Mobile phone using delegates and events. Here each operation is treated separately like addition(also other math operations) is treated as one event, likewise,Character, number and Equalto is treated as another events. The approriate event should raise whenever the button keypress action is done.This should be done using C# Windows Application in .net 2.0.
I did for number event, but stucked in between for math operations and character events( here the action is like typing sms in a mobile phone).
Till now I tried like this..
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
namespace NumberPad
{
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
public delegate void NumberArgumentHandle(object source,NumberArgs args);
public delegate void MathOperationHandler(object sender,Math m);
// public delegate void EqualtoHandler(object sender, EventArgs e);
public delegate void CharacterArgumentHandler(object sender, EventArgs e );
public object key = new object();
public event NumberArgumentHandle NumberSelected
{
add
{
Events.AddHandler(key, value);
}
remove
{
Events.RemoveHandler(key, value);
}
}
public event MathOperationHandler op
{
add
{
Events.AddHandler(key, value);
}
remove
{
Events.RemoveHandler(key, value);
}
}
public event CharacterArgumentHandler c
{
add
{
Events.AddHandler(key, value);
}
remove
{
Events.RemoveHandler(key, value);
}
}
private void btn_key(object sender, KeyPressEventArgs e)
{
int x = int.MaxValue;
int.TryParse((sender as Button).Text, out x);
if (x != int.MaxValue)
{
NumberArgs args = new NumberArgs(Convert.ToInt32((sender as Button).Text));
RaiseEvent(args);
}
// for storing first number
}
private void RaiseEvent(NumberArgs args)
{
if (Events[key] != null)
{
(Events[key] as NumberArgumentHandle)(this, args);
}
}
/* private void RaiseEvent(Math m)
{
if (Events[key] != null)
{
(Events[key] as MathOperationHandler)(this, m);
}
}*/
// calculator controls.
private double Additions(double value1, double value2)
{
return (value1 + value2);
}
/*private int Additions(int value1, int value2)
{
return (value1 + value2);
}*/
private double Subtraction(double value3, double value4)
{
return (value3 - value4);
}
/* private double Subtraction(int value1, int value2)
{
return (value1 - value2);
}*/
private double Multiplication(double value5, double value6)
{
return (value5 * value6);
}
/* private double Multiplication(int value1, int value2)
{
return (value1 * value2);
}*/
private double Division(double value7, double value8)
{
return (value7 * value8);
}
/* private double Division(int value1, int value2)
{
return (value1 * value2);
}*/
// cell controls.
}
public class NumberArgs : EventArgs
{
int number = int.MaxValue;
public NumberArgs(int number)
{
this.number = number;
}
public int Number
{
get{return number;}
}
}
}
Please help me....
Loading
Danatas GerviPosted Aug 31, 2009, 12:37 PM
I see, that it is hard task.
All, that you try to do, is possible to make simpler. By my opinion, you should not use your own delegates witn same parameters. It is "to mach".
Calculator – will not work correctly – if you will desine it so primitive – normal calculator needs the using the stack of values and so on…
:(
I repeat - You should use state machine pattern for you task. According the states – the key press event handler will decide – which char to take, and which method to call.
Sorry if it not sound nice for you. If you want - It may be my last answer to you.
Regards.