• Martes 14 de Mayo de 2024, 23:58

Autor Tema:  Problema para sumar datos en c#  (Leído 2053 veces)

th3r0rn

  • Miembro activo
  • **
  • Mensajes: 49
  • Nacionalidad: mx
    • Ver Perfil
    • http://imgeek.net
Problema para sumar datos en c#
« en: Sábado 20 de Febrero de 2010, 05:25 »
0
Hola, no puedo sumar numeros en c# nose si esta algo mal en mi codigo, es muy simple. espero ustedes puedan orientarme...
Código: C#
  1. private void button1_Click(object sender, EventArgs e)
  2.         {
  3.             int num1, num2,total;
  4.            num1 = (Int32.Parse(textBox1.Text));
  5.            num2 = (Int32.Parse(textBox2.Text));
  6.           total = (Int32.Parse(textBox3.Text));
  7.           total = num1 + num2;
  8.                        
  9.         }
  10.  

tannke

  • Miembro MUY activo
  • ***
  • Mensajes: 152
  • Nacionalidad: es
    • Ver Perfil
Re: Problema para sumar datos en c#
« Respuesta #1 en: Sábado 20 de Febrero de 2010, 11:51 »
0
La suma la haces bien, solo que te sobra un paso y te falta uno:

Código: C#
  1. private void button1_Click(object sender, EventArgs e)
  2.         {
  3.             int num1, num2,total;
  4.            num1 = (Int32.Parse(textBox1.Text));
  5.            num2 = (Int32.Parse(textBox2.Text));
  6.           //total = (Int32.Parse(textBox3.Text)); este lo quitamos,  en el texbox3 no hay nada en teoria
  7.           total = num1 + num2;
  8.            
  9.  
  10.           textBox3.Text = total.ToString();  //Aqui asignamos el resultado a texbox3 convirtiendolo en string
  11.         }
  12.  

metodo abreviado:
Código: C#
  1. private void button1_Click(object sender, EventArgs e)
  2.         {
  3.            try
  4.             {
  5.                 textBox3.Text = (Int32.Parse(textBox1.Text) + Int32.Parse(textBox2.Text)).ToString();
  6.             }
  7.             catch {  textBox3.Text ="Error"; }
  8.         }
  9.  


saludos