Quantcast
Viewing latest article 2
Browse Latest Browse All 15

Calculadora en C# [Error]

Esta seria la corecion.

using System;
using System.Windows.Forms;namespaceWindowsFormsApplication21
{
   
publicpartialclassForm1:Form { float resultado = 0; //string error1 = "+"; bool suma, resta, multi, div; public Form14() { InitializeComponent(); } private void btnmas_Click(object sender, EventArgs e) { float x; if (!float.TryParse(textBox1.Text, out x)) { MessageBox.Show("Valor no permitido"); } else { textBox1.Text = string.Empty; resultado = resultado + x; suma = true; textBox1.Focus(); } } private void btnmenos_Click(object sender, EventArgs e) { float x; if (!float.TryParse(textBox1.Text, out x)) { MessageBox.Show("Valor no permitido"); } else { resultado = resultado + x; resta = true; textBox1.Focus(); textBox1.Text = string.Empty; } } private void btnpor_Click(object sender, EventArgs e) { float x; if (!float.TryParse(textBox1.Text, out x)) { MessageBox.Show("Valor no permitido"); } else { resultado = resultado + x; multi = true; textBox1.Focus(); textBox1.Text = string.Empty; } } private void btndividio_Click(object sender, EventArgs e) { float x; if (!float.TryParse(textBox1.Text, out x)) { MessageBox.Show("Valor no permitido"); } else { resultado = resultado + x; div = true; textBox1.Focus(); textBox1.Text = string.Empty; } } private void btnigual_Click(object sender, EventArgs e) { float x; bool valor = float.TryParse(textBox1.Text, out x); if (valor) { if (suma) { resultado += x; } if (resta) { resultado -= x; } if (multi) { resultado *= x; } if (div) { resultado /= x; } } else { MessageBox.Show("Error en los valores"); } textBox1.Text = resultado.ToString(); suma = false; resta = false; multi = false; div = false; resultado = 0; } private void btnborrar_Click(object sender, EventArgs e) { textBox1.Clear(); textBox1.Focus(); } private void textBox1_TextChanged(object sender, EventArgs e) { } private void textBox1_KeyPress(object sender, KeyPressEventArgs e) { if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar)&& e.KeyChar != ',') { e.Handled = true; } if (e.KeyChar == ',' && (sender as TextBox).Text.IndexOf(',') > -1) { e.Handled = true; } } } }

Ya se porque no havia dado error, y era porque ya habia comprobado el valor del textbox en los metodo manejador para: la suma, resta, mult y div

Estoy deacuerdo con tigo en que el mejor mecanismo para detectar y reparar los errores en usando bloque TRY....CATCH


Angel R. Jimenez G.
Software Development
Santo Domingo
Republica Dominicana
Blog
Image may be NSFW.
Clik here to view.

Viewing latest article 2
Browse Latest Browse All 15