Somar 5 TextBox e apresentar no ¨6 Texbox

.NET

20/05/2011

Pessoal preciso de um help no sentido e ir calculando o total conforme o usuário inseri os valores em 5 TextBox, e será apresentando o Totla no TextBox 6, pode ser JavaScript ou In Side, via alguns exemplos na Net, mas nenhum deu certo, não sei se pelo fato de usar VS Studio, ou inexperiencia minha. Valeu
Paulo
Paulo Freire

Paulo Freire

Curtidas 0

Respostas

Samuel

Samuel

20/05/2011

Olá Paulo, isto seria na plataforma desktop?
Segue abaixo um exemplo simples que acabei de contruir:
 private void Soma_equanto_digita(object sender, EventArgs e)        {            long tx1, tx2, tx3, tx4, tx5, txResultado;            if (textBox1.Text.Length > 0)            {                tx1 = long.Parse(textBox1.Text);            }            else            {                MessageBox.Show("Insira um Valor numérico");                tx1 = 0;            }            if (textBox2.Text.Length >0)            {                tx2 = long.Parse(textBox2.Text);            }            else            {                tx2 = 0;            }            if (textBox3.Text.Length > 0)            {                tx3 = long.Parse(textBox3.Text);            }            else            {                tx3 = 0;            }            if (textBox4.Text.Length > 0)            {                tx4 = long.Parse(textBox4.Text);            }            else            {                tx4 = 0;            }            if (textBox5.Text.Length > 0)            {                tx5 = long.Parse(textBox5.Text);                           }            tx5 = 0;                       txResultado = tx1 + tx2 + tx3 + tx4 + tx5;            textBox6.Text = txResultado.ToString();
        }
Lembre-se de atribuir este metodo nas propriedades de execução dos txtbox na parte TextChanged
Sds,Samuel
GOSTEI 0
Paulo Freire

Paulo Freire

20/05/2011

Valeu Sam, era pra WebForm, esqueci de mencionar, mas vc me deu uma luz, onde procurei no google e encontrei um que deu certo.


public void Calcula()
        {
            try
            {
                //took an integer variable which will store the result of addition
                float radix = float.Parse(txt_L50.Text.Replace(',', '.')) + float.Parse(txt_L20.Text.Replace(',', '.')) + float.Parse(txt_L10.Text.Replace(',', '.')) + float.Parse(txt_L5.Text.Replace(',', '.')) + float.Parse(txt_L2.Text.Replace(',', '.')) +
                    float.Parse(txt_L1.Text.Replace(',', '.')) + float.Parse(txt_P50.Text.Replace(',', '.')) + float.Parse(txt_P20.Text.Replace(',', '.')) + float.Parse(txt_P10.Text.Replace(',', '.')) + float.Parse(txt_P5.Text.Replace(',', '.')) +
                    float.Parse(txt_P2.Text.Replace(',', '.')) + float.Parse(txt_P1.Text.Replace(',', '.'));
                //print the result in the textbox6 as per your requirement
                //txtTotal.Text = radix.ToString();
               
                txtTotal.Text = string.Format("{0:c}", radix.ToString());

               
            }
            catch (Exception ex)
            {
                //if error comes then this block of code will be executed               
                lblMensagem.Text = "Numeros invalido...";
            }
        }
GOSTEI 0
POSTAR