Click here to Skip to main content
15,896,118 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
Button[] numbers = new Button[10];
Button[] operators = new Button[5];
Button[] soperators = new Button[15];
string[] symbols = new string[]{ "+", "-", "*", "/" ,"="};
string[] scientific = new string[] {"sin","cos","tan","log","e","x2","^","!","(",")","[","]","Ans","rad","exp" };
public Form1()
{
    InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
    for (int i = 0; i < 10; i++)
    {
        if (i < 9)
        {
            numbers[i] = new Button();
            numbers[i].Text = (i + 1).ToString();
            numbers[i].Size = new Size(40, 40);
        }
        else
        {
            numbers[9] = new Button();
            numbers[9].Text = "0";
            numbers[9].Size = new Size(130, 40);
        }
        numbers[i].Click += btn_Click;
        this.flowLayoutPanel2.Controls.Add(numbers[i]);
    }
    for (int i = 0; i < 5; i++)
    {
        operators[i] = new Button();
        operators[i].Text = symbols[i];
        operators[i].Click += op_Click;
        this.flowLayoutPanel3.Controls.Add(operators[i]);
    }

    for (int i = 0; i < 15; i++)
    {
        soperators[i] = new Button();
        soperators[i].Text = scientific[i];
        soperators[i].Size = new Size(50, 30);
        this.flowLayoutPanel1.Controls.Add(soperators[i]);
    }
}
public void btn_Click(object sender,EventArgs e)
{
    Button b = (Button)sender;
    textBox1.Text = textBox1.Text + b.Text;
}
int firstNo;int secondNo=0;
string si;
public void op_Click(object sender,EventArgs e)
{
    Int32 result;
    Button b = (Button)sender;
    si = b.Text;
    firstNo = int.Parse(textBox1.Text);
    result = firstNo;
    textBox1.Text = "";
    secondNo = int.Parse(textBox1.Text);
    if (textBox1.Text!= "")
    {
        switch(si)
        {
            case "+":
                result = firstNo + secondNo;
                break;
            case "-":
                result = firstNo - secondNo;
                break;
            case "*":
                result = firstNo * secondNo;
                break;
            case "/":
                result = firstNo / secondNo;
                break;
            case "=":
                textBox1.Text = result.ToString();
                break;
        }

    }
}
Posted

1 solution

I think you are very close to being able to make this work: just focus on the fact that the moment one of the +-*/ operator buttons is pressed you need:

1. to store the value in the TextBox, as well as which operator was pressed

2. when the = operator button is pressed you need to get the second value, and then execute the stored operator, using the first value entered

I suggest you make the = operator button Click EventHandler a separate function in order to do this. I also suggest you switch to using 'double, not Int32, since you will lose all fractional values using Int32.

Here's a rough outline in psuedo-code:
C#
private double firstValue;

private string operatorToExecute;

private void ActionOperator_Click((object sender, EventArgs e))
{
    operatorToExecute = // get the operator from the button's Text property

    firstValue = // parse the entry TextBox to get the first value

    // clear the entry TextBox
}

private void EqualOperator_Click()
{
    // do you need to check and see if you have a valid action operator ?
    // do you need to check and see if you have a valid first value ?
    // do you need to handle more than one click at a time of this button ?
    
    double secondValue = // get the second value from the entry TextBox
    
    // switch statement goes here to execute action operator
    
    // display result or error message
}
 
Share this answer
 
Comments
Maciej Los 25-Oct-14 16:35pm    
Good advice, +5!
Sergey Alexandrovich Kryukov 26-Oct-14 0:48am    
5ed.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900