A Continuación os pongo el código completo. Como en las imágenes esta todo bien comentado y marcado. No veo que haga falta mayores explicaciones.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace calculadora_1
{
public partial class Calculadora : Form
{
public Calculadora()
{
InitializeComponent();
}
//Creo el Boolenao, para disinguir operando 1, del 2 en el teclado.
Boolean click;
private void txtoperando1_Click(object sender, EventArgs e)
{
click = false;
}
private void txtOperando2_Click(object sender, EventArgs e)
{
click = true;
}
//Operandos
int n1=0, n2=0;
string operacion;
//resultado
private void txtresultado_TextChanged(object sender, EventArgs e)
{
txtresultado.ForeColor = Color.Red;
switch (operacion)
{
case "+":
txtresultado.Text = (n1 + n2).ToString();
break;
case "-":
txtresultado.Text = (n1 - n2).ToString();
break;
case "*":
txtresultado.Text = (n1 * n2).ToString();
break;
case "/":
txtresultado.Text = (n1 / n2).ToString();
break;
}
}
//operando1
private void txtoperando1_TextChanged(object sender, EventArgs e)
{
}
//operando2
private void txtOperando2_TextChanged(object sender, EventArgs e)
{
}
//Teclado
//Borrar
private void btnBorrar_Click_1(object sender, EventArgs e)
{
txtoperando1.Text=" ";
txtOperando2.Text=" ";
txtresultado.Text=" ";
n1 = 0;
n2 = 0;
operacion = "?";
click = false;
}
//1
private void btn1_Click(object sender, EventArgs e)
{
if (click==false)
{
txtoperando1.Text += 1;
}
else
{
txtOperando2.Text += 1;
}
}
//2
private void btn2_Click_1(object sender, EventArgs e)
{
if (click == false)
{
txtoperando1.Text += 2;
}
else
{
txtOperando2.Text += 2;
}
}
//3
private void btn3_Click_1(object sender, EventArgs e)
{
if (click == false)
{
txtoperando1.Text += 3;
}
else
{
txtOperando2.Text += 3;
}
}
//4
private void btn4_Click_1(object sender, EventArgs e)
{
if (click == false)
{
txtoperando1.Text += 4;
}
else
{
txtOperando2.Text += 4;
}
}
//5
private void btn5_Click(object sender, EventArgs e)
{
if (click == false)
{
txtoperando1.Text += 5;
}
else
{
txtOperando2.Text += 5;
}
}
//6
private void btn6_Click(object sender, EventArgs e)
{
if (click == false)
{
txtoperando1.Text += 6;
}
else
{
txtOperando2.Text += 6;
}
}
//7
private void btn7_Click(object sender, EventArgs e)
{
if (click == false)
{
txtoperando1.Text += 7;
}
else
{
txtOperando2.Text += 7;
}
}
//8
private void btn8_Click(object sender, EventArgs e)
{
if (click == false)
{
txtoperando1.Text += 8;
}
else
{
txtOperando2.Text += 8;
}
}
//9
private void btn9_Click(object sender, EventArgs e)
{
if (click == false)
{
txtoperando1.Text += 9;
}
else
{
txtOperando2.Text += 9;
}
}
//0
private void button11_Click(object sender, EventArgs e)
{
if (click == false)
{
txtoperando1.Text += 0;
}
else
{
txtOperando2.Text += 0;
}
}
//+
private void btnSumar_Click(object sender, EventArgs e)
{
n1 = int.Parse(txtoperando1.Text);
operacion = "+";
click = true;
}
//-
private void btnRestar_Click(object sender, EventArgs e)
{
n1 = int.Parse(txtoperando1.Text);
operacion = "-";
click = true;
}
//*
private void btnMultiplicar_Click(object sender, EventArgs e)
{
n1 = int.Parse(txtoperando1.Text);
operacion = "*";
click = true;
}
//'/'
private void btnDividir_Click(object sender, EventArgs e)
{
n1 = int.Parse(txtoperando1.Text);
operacion = "/";
click = true;
}
//=
private void btnResultado_Click(object sender, EventArgs e)
{
n2 = int.Parse(txtOperando2.Text);
txtresultado.Text = txtresultado.ToString();
}
}
}
0 comentarios:
Publicar un comentario