TextBox_Changed is accumulating the value of the sum c #

3

I am using the textBox_Changed event and when I type a value in the field, another textBox should receive this value, but this other textBox called ValorTotalVenda is accumulating this value.

private void textBox4_TextChanged(object sender, EventArgs e)
{
    if (textBox4.Text != "")
    {
         venda.ValorAcrescimo = Convert.ToDecimal(textBox4.Text);
         venda.ValorTotalVenda += venda.ValorAcrescimo;
         textBox6.Text = Convert.ToString(venda.ValorTotalVenda);
     }
}
    
asked by anonymous 11.01.2017 / 14:36

2 answers

4

You are changing the value of the ValueTotalVenda property, would not it be easier just to add the values to the textbox6? the way it is written or you define a global variable for the initial value of the sale or you do it that way.

if (textBox4.Text != "")
        {
            venda.ValorAcrescimo = Convert.ToDecimal(textBox4.Text);
            textBox6.Text = Convert.ToString(venda.ValorTotalVenda + venda.ValorAcrescimo);
        }
    
11.01.2017 / 14:58
1

Each character typed in textoBox4 is added to the value of the ValorTotalVenda variable, which strikes me as strange. You asked to display the added value in textbox6, since operador + with numbers will perform an addition, not a concatenation.

private void textBox4_TextChanged(object sender, EventArgs e)
    {
        if (textBox4.Text != "")
        {
            venda.ValorAcrescimo = Convert.ToDecimal(textBox4.Text);
            venda.ValorTotalVenda += venda.ValorAcrescimo;
            //Mostra o valor somado
            textBox6.Text = venda.ValorTotalVenda.ToString();
            //ou, mostra inteiramente o conteúdo que está sendo digitado no textbo4
            textBox6.Text = textBox4.Text;
        }
    }

In addition to the code, see these posts as a complement:


What is the most appropriate way to concatenate strings?

    
11.01.2017 / 15:16