Do 3 different operations with a single C # button

0

I hope you can help me please, I want to perform the calculation of these 3 operations (2 divisions and 1 multiplication) with a single button (so I do not have to do them separately) but I do not know how to do it. My code is as follows:

private void btnCalcular_Click(object sender, EventArgs e)
{
    double fijo = double.Parse(lblfijo.Text);
    double num3 = double.Parse(txtTC.Text); 
    double div = fijo / num3;
    txtResultado.Text = div.ToString(); 
}

private void btnCalcular2_Click(object sender, EventArgs e)
{
    double resul1 = double.Parse(txtResultado.Text);
    double num2 = double.Parse(txtEFF.Text);
    double mul = resul1 * num2;
    txtResultado2.Text = mul.ToString();
}

private void btnCalcular3_Click(object sender, EventArgs e)
{
    double operador = double.Parse(txtoperador.Text);
    double resul2 = double.Parse(txtResultado2.Text);
    double div = operador / resul2;
    txtResultado3.Text = div.ToString();
}

PS: I use "double" for the numbers with a decimal point since the value of txtEFF.Text is a percentage.

    
asked by Eduardo Parra 09.11.2016 в 02:49
source

2 answers

2

You can keep the separate functionality in three different methods as you have in your original code, all you have to do is add the three event handler the click event of your button.

button.OnClick += new EventHandler(btnCalcular_Click);
button.OnClick += new EventHandler(btnCalcular2_Click);
button.OnClick += new EventHandler(btnCalcular3_Click);

Remember that all the events that are triggered support having more than one subscriber, not just the clicker. It is also important to clarify that if any of your methods had dependencies with others, for example btnCalcular2_Click uses the result of btnCalculate_Click to calculate something, you should not add both handler separately because having them subscribed in that order does not mean that they will be executed in that way .

    
answered by 09.11.2016 в 19:38
1

You can do the following

private void btnCalcular_Click(object sender, EventArgs e)
{
    double fijo = double.Parse(lblfijo.Text);
    double num3 = double.Parse(txtTC.Text); 
    double div = fijo / num3;
    txtResultado.Text = div.ToString(); 
    double resul1 = div;
    double num2 = double.Parse(txtEFF.Text);
    double mul = resul1 * num2;
    txtResultado2.Text = mul.ToString();
    double operador = double.Parse(txtoperador.Text);
    double resul2 = mul;
    double div = operador / resul2;
    txtResultado3.Text = div.ToString();
}

Put all the operations on the button that you want you to do. Since you have the double variables of the results of each operation, you save the parsing of the value that you leave in the txtResultado 1, 2 and 3 and occupy the variables that you already started with the results.

    
answered by 09.11.2016 в 02:55