15,961,188 members
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();
}

private void Operations()
{

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

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

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

//Minus
{
if (btnMinus.Click

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

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

}

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

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

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

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

}

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

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

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

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

}

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;
string szLeft = "";
string szRight = "";
string szEquation = "";

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

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

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

}
}
}

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

## Solution 1

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;
...
switch (op)
{
case Ops.Minus:
break;
...
}
}

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