Práctica 6. Calculadora básica
Objetivo: Diseñar una aplicación en C# que emule una calculadora básica: sume, reste, multiplique y divida dos números. Debe cumplir con las siguientes características:
- Al ejecutar la aplicación deben estar ocultos la etiqueta y la caja de texto del resultado
- Cuando se presione un botón que se muestre el resultado y en la etiqueta uno de los siguientes mensajes: La suma es, la diferencia es, el producto es, el cociente es, dependiendo del botón presionado.
- El usuario no podrá modificar el resultado de la operación
- En las cajas de texto solo se podrán permitir escribir números
- Incluir un botón C para limpiar las cajas de texto y volver a ocultar la etiqueta y el resultado.
Código:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Pratica_6
{
public partial class FrmCalculadora : Form
{
public FrmCalculadora()
{
InitializeComponent();
}
private void textBox1_KeyPress(object sender,KeyPressEventArgs e)
{
if (Char.IsDigit(e.KeyChar))
{
e.Handled = false;
}
else if (Char.IsControl(e.KeyChar))
{
e.Handled = false;
}
else
{
e.Handled = true;
}
}
private void textBox2_KeyPress(object sender,KeyPressEventArgs e)
{
if (Char.IsDigit(e.KeyChar))
{
e.Handled = false;
}
else if (Char.IsControl(e.KeyChar))
{
e.Handled = false;
}
else
{
e.Handled = true;
}
}
private void btnSuma_Click(object sender, EventArgs e)
{
double primero, segundo, suma;
primero = Convert.ToDouble(textBox1.Text);
segundo = Convert.ToDouble(textBox2.Text);
suma = primero + segundo;
textBox3.Text = Convert.ToString(suma);
textBox3.Visible = true;
textBox3.Enabled = false;
lbtexto.Visible = true;
lbtexto.Text = "La suma es:";
}
private void btnResta_Click(object sender, EventArgs e)
{
double primero, segundo, suma;
primero = Convert.ToDouble(textBox1.Text);
segundo = Convert.ToDouble(textBox2.Text);
suma = primero - segundo;
textBox3.Text = Convert.ToString(suma);
textBox3.Visible = true;
textBox3.Enabled = false;
lbtexto.Text = "La diferencia es:";
}
private void btnPor_Click(object sender, EventArgs e)
{
double primero, segundo, suma;
primero = Convert.ToDouble(textBox1.Text);
segundo = Convert.ToDouble(textBox2.Text);
suma = primero * segundo;
textBox3.Text = Convert.ToString(suma);
textBox3.Visible = true;
textBox3.Enabled = false;
lbtexto.Text = "El producto es:";
}
private void btndiv_Click(object sender, EventArgs e)
{
double primero, segundo, suma;
primero = Convert.ToDouble(textBox1.Text);
segundo = Convert.ToDouble(textBox2.Text);
suma = primero / segundo;
textBox3.Text = Convert.ToString(suma);
textBox3.Visible = true;
textBox3.Enabled = false;
lbtexto.Text = "El cociente es:";
}
private void btnC_Click(object sender, EventArgs e)
{
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
textBox3.Visible = false
lbtexto.Visible = false;
}
}
}
No hay comentarios:
Publicar un comentario