Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to build a calculator and put the code that runs it inside a method Operations() and when the - minus is clicked for example it will look inside Operations() and then see the if btnMinus.click and will run that part of it. I also need to do this +, /, *, percent but once I get some help on this I should be able to figure it out.

I am attempting to create a parameter I am confused at how to do this as my book explains it as Minus => 100. The user enters a Left and a Right value. How would I get it to go through the Minus part of the code in the method Operations()?

public frmActors()
{
InitializeComponent();
}

//Put Your method here
private void Operations()
{

//Code all operations will use.
decimal dLeft = 0.0m;
decimal dRight = 0.0m;
decimal dAnswer = 0.0m;
string szLeft = "";
string szRight = "";
string szAnswer = "";
string szEquation = "";

szLeft = txtLeft.Text;
szRight = txtRight.Text;

dLeft = Convert.ToDecimal(szLeft);
dRight = Convert.ToDecimal(szRight);



//Minus
{
if (btnMinus.Click
)dAnswer = dLeft - dRight;

szAnswer = dAnswer.ToString();

szEquation = szLeft + " - " + szRight + " = " + szAnswer;
}









lblAnswer.Text = "";
lblAnswer.Text = szEquation;








}
private void btnPercent_Click(object sender, EventArgs e)
{







}



private void btnDivide_Click(object sender, EventArgs e)
{
decimal dLeft = 0.0m;
decimal dRight = 0.0m;
decimal dAnswer = 0.0m;
string szLeft = "";
string szRight = "";
string szAnswer = "";
string szEquation = "";

szLeft = txtLeft.Text;
szRight = txtRight.Text;

dLeft = Convert.ToDecimal(szLeft);
dRight = Convert.ToDecimal(szRight);

dAnswer = dLeft / dRight;

szAnswer = dAnswer.ToString();

szEquation = szLeft + " / " + szRight + " = " + szAnswer;

lblAnswer.Text = "";
lblAnswer.Text = szEquation;


}

private void btnMultiply_Click(object sender, EventArgs e)
{
decimal dLeft = 0.0m;
decimal dRight = 0.0m;
decimal dAnswer = 0.0m;
string szLeft = "";
string szRight = "";
string szAnswer = "";
string szEquation = "";

szLeft = txtLeft.Text;
szRight = txtRight.Text;

dLeft = Convert.ToDecimal(szLeft);
dRight = Convert.ToDecimal(szRight);

dAnswer = dLeft * dRight;

szAnswer = dAnswer.ToString();

szEquation = szLeft + " * " + szRight + " = " + szAnswer;

lblAnswer.Text = "";
lblAnswer.Text = szEquation;


}

private void btnMinus_Click(object sender, EventArgs e)
{
Operations();


}

private void btnPlus_Click(object sender, EventArgs e)
{
decimal dLeft = 0.0m;
decimal dRight = 0.0m;
decimal dAnswer = 0.0m;
string szLeft = "";
string szRight = "";
string szAnswer = "";
string szEquation = "";

szLeft = txtLeft.Text;
szRight = txtRight.Text;

dLeft = Convert.ToDecimal(szLeft);
dRight = Convert.ToDecimal(szRight);

dAnswer = dLeft + dRight;

szAnswer = dAnswer.ToString();

szEquation = szLeft + " + " + szRight + " = " + szAnswer;

lblAnswer.Text = "";
lblAnswer.Text = szEquation;

}
}
}

What I have tried:

I have tried creating a parameter by using the btnMinus.click to enable it to go through that set of code.
I am needing to do +, *, /, and percent also that is why I need to do it inside a method. I am just unsure how after I put it in there to call it.
Posted
Updated 10-Oct-17 13:00pm

1 solution

Here is a solution to your specific question.
I am not discussing, if this is the most logical way of doing this, but just provide an answer to what you try to accomplish in your way.

One of the reason your code does not work is that btnMinus.Click is not what you need, it is not telling you if the button was clicked, but it is an event handler that contains the callback method that gets called when the button is pressed. InitializeComponent adds the callback like this:
this.btnMinus.Click += new System.EventHandler(this.btnMinus_Click);

This much just as explanation.
So you need a different way to tell Operations() which operation to perform. One way is propsed here (I just put the minus operation code as example, you should be able to implement the other operations):
        /// <summary>
        /// Define all operations you want to support in this enumeration
        /// </summary>
        enum Ops
            {
                Plus,
                Minus,
                Mult,
                Div,
                Percent
            };

        /// <summary>
        /// Do all work in this single method.
        /// </summary>
        /// <param name="op">The operation to perform.</param>
        void Operations(Ops op)
        {
            decimal dLeft = 0.0m;
            decimal dRight = 0.0m;
            decimal dAnswer = 0.0m;
...
            switch (op)
            {
                case Ops.Minus:
                    dAnswer = dLeft - dRight;
                    break;
...
            }
        }

        private void btnMinus_Click(object sender, EventArgs e)
        {
            Operations(Ops.Minus);
        }
...
 
Share this answer
 

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