Click here to Skip to main content
15,896,453 members
Home / Discussions / C#
   

C#

 
QuestionSimple calculator Pin
nstk12-Feb-11 7:28
nstk12-Feb-11 7:28 
AnswerRe: Simple calculator PinPopular
Luc Pattyn12-Feb-11 7:37
sitebuilderLuc Pattyn12-Feb-11 7:37 
GeneralRe: Simple calculator Pin
nstk12-Feb-11 11:07
nstk12-Feb-11 11:07 
AnswerRe: Simple calculator Pin
Manfred Rudolf Bihy12-Feb-11 11:19
professionalManfred Rudolf Bihy12-Feb-11 11:19 
AnswerRe: Simple calculator Pin
Luc Pattyn12-Feb-11 11:21
sitebuilderLuc Pattyn12-Feb-11 11:21 
GeneralRe: Simple calculator Pin
nstk13-Feb-11 23:57
nstk13-Feb-11 23:57 
GeneralRe: Simple calculator [modified] Pin
nstk14-Feb-11 3:52
nstk14-Feb-11 3:52 
AnswerRe: Simple calculator Pin
DaveyM6912-Feb-11 12:22
professionalDaveyM6912-Feb-11 12:22 
If you're using Visual Studio, select the button then in the Properties window click the events icon.
In the dropdown next to the event (Click in this case) you can select any method with the correct signature. Selecting the same method for all button's click event will result in the same handler being called.

To get the value, as Luc said you can use the Tag property - although I would be tempted to add a more suitable property such as Value which would hold an int... something like this:
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;

public class CalculatorDigitButton : Button
{
    public const int MaxValue = 9;
    public const int MinValue = 0;

    public CalculatorDigitButton()
    {
        base.Text = "0";
    }

    private int value;

    [Browsable(false)]
    public new string Text
    {
        get { return base.Text; }
        set { }
    }
    public int Value
    {
        get { return value; }
        set
        {
            if (value < MinValue || value > MaxValue)
                throw new ArgumentOutOfRangeException(
                    "value",
                    string.Format("Value must be between {0} and {1}",
                        MinValue, MaxValue));
            this.value = value;
            base.Text = value.ToString();
        }
    }
}

public enum CalculatorFunction
{
    Add,
    Subtract,
    Multiply,
    Divide
}

public class CalculatorFunctionButton : Button
{
    private static readonly List<string> FunctionText = new List<string>(4){
        "+", "-", "*", "/" };

    private CalculatorFunction function;

    public CalculatorFunctionButton()
    {
        base.Text = FunctionText[0];
    }

    public CalculatorFunction Function
    {
        get { return function; }
        set
        {
            if (!Enum.IsDefined(typeof(CalculatorFunction), value))
                throw new ArgumentException(
                    "Function must be a defined value", "Function");
            function = value;
            base.Text = FunctionText[(int)function];
        }
    }
    [Browsable(false)]
    public new string Text
    {
        get { return base.Text; }
        set { }
    }
}

Your event handlers for these may look something like this:
C#
// Assign each digit button's click event to this handler
private void CalculatorDigitButtonClick(object sender, EventArgs e)
{
    CalculatorDigitButton calculatorDigitButton = sender as CalculatorDigitButton;
    if (calculatorDigitButton != null)
    {
        // Do your thing, value is in calculatorDigitButton.Value
    }
}
// Assign each function button's click event to this handler
private void CalculatorFunctionButtonClick(object sender, EventArgs e)
{
    CalculatorFunctionButton calculatorFunctionButton = sender as CalculatorFunctionButton;
    if (calculatorFunctionButton != null)
    {
        // Do your thing, function is in calculatorFunctionButton.Function
    }
}

Dave

Binging is like googling, it just feels dirtier.
Please take your VB.NET out of our nice case sensitive forum.
Astonish us. Be exceptional. (Pete O'Hanlon)

BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)



AnswerRe: Simple calculator Pin
Luc Pattyn12-Feb-11 18:04
sitebuilderLuc Pattyn12-Feb-11 18:04 
GeneralRe: Simple calculator Pin
DaveyM6913-Feb-11 2:48
professionalDaveyM6913-Feb-11 2:48 
AnswerRe: Simple calculator Pin
Luc Pattyn13-Feb-11 3:01
sitebuilderLuc Pattyn13-Feb-11 3:01 
GeneralRe: Simple calculator [modified] Pin
nstk14-Feb-11 10:21
nstk14-Feb-11 10:21 
AnswerRe: Simple calculator Pin
RichardGrimmer15-Feb-11 3:58
RichardGrimmer15-Feb-11 3:58 
Questionerror in timer function Pin
aeman12-Feb-11 4:40
aeman12-Feb-11 4:40 
AnswerRe: error in timer function Pin
Manfred Rudolf Bihy12-Feb-11 5:10
professionalManfred Rudolf Bihy12-Feb-11 5:10 
GeneralRe: error in timer function Pin
aeman12-Feb-11 6:25
aeman12-Feb-11 6:25 
AnswerRe: error in timer function Pin
OriginalGriff12-Feb-11 5:20
mveOriginalGriff12-Feb-11 5:20 
GeneralRe: error in timer function Pin
aeman12-Feb-11 20:07
aeman12-Feb-11 20:07 
AnswerRe: error in timer function Pin
OriginalGriff12-Feb-11 21:34
mveOriginalGriff12-Feb-11 21:34 
AnswerRe: error in timer function Pin
Henry Minute12-Feb-11 6:07
Henry Minute12-Feb-11 6:07 
AnswerRe: error in timer function Pin
Luc Pattyn12-Feb-11 6:14
sitebuilderLuc Pattyn12-Feb-11 6:14 
GeneralRe: error in timer function Pin
Henry Minute12-Feb-11 6:34
Henry Minute12-Feb-11 6:34 
GeneralRe: error in timer function Pin
Luc Pattyn12-Feb-11 6:42
sitebuilderLuc Pattyn12-Feb-11 6:42 
AnswerRe: error in timer function Pin
_Erik_14-Feb-11 3:42
_Erik_14-Feb-11 3:42 
Questioncapture finger print continuously Pin
s_akram12-Feb-11 3:12
s_akram12-Feb-11 3:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.