
Seguimos desarrollando nuestra calculadora básica que comenzamos en el post anterior (Crear Calculadora con WPF y C# (Primera Parte))
1.- A cada elemento le asignaremos un nombre para poder identificarlo correctamente. Utilizaremos x:Name="nombre" para esta asignación. Lo realizamos en el XAML:
Botones:
<!-- Botones: Números -->
<button grid.column="0" grid.row="2" x:name="btn7">7</button>
<button grid.column="1" grid.row="2" x:name="btn8">8</button>
<button grid.column="2" grid.row="2" x:name="btn9">9</button>
<button grid.column="0" grid.row="3" x:name="btn4">4</button>
<button grid.column="1" grid.row="3" x:name="btn5">5</button>
<button grid.column="2" grid.row="3" x:name="btn6">6</button>
<button grid.column="0" grid.row="4" x:name="btn1">1</button>
<button grid.column="1" grid.row="4" x:name="btn2">2</button>
<button grid.column="2" grid.row="4" x:name="btn3">3</button>
<button grid.column="1" grid.row="5" x:name="btn0">0</button>
<!-- Botones: Operadores -->
<button grid.column="3" grid.row="1" x:name="btnSumar">+</button>
<button grid.column="3" grid.row="2" x:name="btnRestar">-</button>
<button grid.column="3" grid.row="3" x:name="btnMultiplicar">*</button>
<button grid.column="3" grid.row="4" x:name="btnDividir">/</button>
<button grid.column="3" grid.row="5" x:name="btnResultado">=</button>
<!-- Botones: Limpiadores -->
<button grid.column="0" grid.row="1" x:name="btnCE">CE</button>
<button grid.column="1" grid.row="1" x:name="btnC">C</button>
<button content="<=" grid.column="2" grid.row="1" x:name="btnRetroceder"></button>
<!-- Boton: Positivo y Negativo -->
<button content="+-" grid.column="0" grid.row="5" x:name="btnPositivoNegativo"></button>
TextBox:
<!-- TextBox -->
<TextBox x:Name="txtResultado" IsReadOnly="True" TextAlignment="Right" VerticalContentAlignment="Bottom" FontSize="40" Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="4">0</TextBox>
Ahora comenzaremos con la programación en C#
2.- Comenzamos a programar. Vamos a dar clic en el botón 1, En el panel de Propiedades seleccionamos el control de eventos y damos doble clic en "Clic".
3.- Creamos tres variables (para almacenar los números y la operación)
4.- En la opción Clic del botón 1 insertamos:
private void btn1_Click(object sender, RoutedEventArgs e)
{
if (operacion == "")
{
numero1 = (numero1 * 10) + 1;
txtResultado.Text = numero1.ToString();
}
else
{
numero2 = (numero1 * 10) + 1;
txtResultado.Text = numero1.ToString();
}
}
5.- Repetimos este proceso por cada botón numérico, hasta el momento tendríamos:
namespace Calculadora
{
///
/// Lógica de interacción para MainWindow.xaml
///
public partial class MainWindow : Window
{
long numero1 = 0;
long numero2 = 0;
string operacion = "";
public MainWindow()
{
InitializeComponent();
}
private void btn1_Click(object sender, RoutedEventArgs e)
{
if (operacion == "")
{
numero1 = (numero1 * 10) + 1;
txtResultado.Text = numero1.ToString();
}
else
{
numero2 = (numero1 * 10) + 1;
txtResultado.Text = numero1.ToString();
}
}
private void btn2_Click(object sender, RoutedEventArgs e)
{
if (operacion == "")
{
numero1 = (numero1 * 10) + 2;
txtResultado.Text = numero1.ToString();
}
else
{
numero2 = (numero1 * 10) + 2;
txtResultado.Text = numero1.ToString();
}
}
private void btn3_Click(object sender, RoutedEventArgs e)
{
if (operacion == "")
{
numero1 = (numero1 * 10) + 3;
txtResultado.Text = numero1.ToString();
}
else
{
numero2 = (numero1 * 10) + 3;
txtResultado.Text = numero1.ToString();
}
}
private void btn4_Click(object sender, RoutedEventArgs e)
{
if (operacion == "")
{
numero1 = (numero1 * 10) + 4;
txtResultado.Text = numero1.ToString();
}
else
{
numero2 = (numero1 * 10) + 4;
txtResultado.Text = numero1.ToString();
}
}
private void btn5_Click(object sender, RoutedEventArgs e)
{
if (operacion == "")
{
numero1 = (numero1 * 10) + 5;
txtResultado.Text = numero1.ToString();
}
else
{
numero2 = (numero1 * 10) + 5;
txtResultado.Text = numero1.ToString();
}
}
private void btn6_Click(object sender, RoutedEventArgs e)
{
if (operacion == "")
{
numero1 = (numero1 * 10) + 6;
txtResultado.Text = numero1.ToString();
}
else
{
numero2 = (numero1 * 10) + 6;
txtResultado.Text = numero1.ToString();
}
}
private void btn7_Click(object sender, RoutedEventArgs e)
{
if (operacion == "")
{
numero1 = (numero1 * 10) + 7;
txtResultado.Text = numero1.ToString();
}
else
{
numero2 = (numero1 * 10) + 7;
txtResultado.Text = numero1.ToString();
}
}
private void btn8_Click(object sender, RoutedEventArgs e)
{
if (operacion == "")
{
numero1 = (numero1 * 10) + 8;
txtResultado.Text = numero1.ToString();
}
else
{
numero2 = (numero1 * 10) + 8;
txtResultado.Text = numero1.ToString();
}
}
private void btn9_Click(object sender, RoutedEventArgs e)
{
if (operacion == "")
{
numero1 = (numero1 * 10) + 9;
txtResultado.Text = numero1.ToString();
}
else
{
numero2 = (numero1 * 10) + 9;
txtResultado.Text = numero1.ToString();
}
}
private void btn0_Click(object sender, RoutedEventArgs e)
{
if (operacion == "")
{
numero1 = (numero1 * 10) + 0;
txtResultado.Text = numero1.ToString();
}
else
{
numero2 = (numero1 * 10) + 0;
txtResultado.Text = numero1.ToString();
}
}
}
}
6.- Botones de operación:
private void btnSumar_Click(object sender, RoutedEventArgs e)
{
operacion = "+";
txtResultado.Text = "0";
}
private void btnRestar_Click(object sender, RoutedEventArgs e)
{
operacion = "-";
txtResultado.Text = "0";
}
private void btnMultiplicar_Click(object sender, RoutedEventArgs e)
{
operacion = "*";
txtResultado.Text = "0";
}
private void btnDividir_Click(object sender, RoutedEventArgs e)
{
operacion = "/";
txtResultado.Text = "0";
}
7.- Botón de resultado:
private void btnResultado_Click(object sender, RoutedEventArgs e)
{
switch(operacion)
{
case "+":
txtResultado.Text = (numero1 + numero2).ToString();
break;
case "-":
txtResultado.Text = (numero1- numero2).ToString();
break;
case "*":
txtResultado.Text = (numero1 * numero2).ToString();
break;
case "/":
txtResultado.Text = (numero1 / numero2).ToString();
break;
}
numero1 = 0;
numero2 = 0;
operacion = "";
}
8.- Botón +-:
private void btnPositivoNegativo_Click(object sender, RoutedEventArgs e)
{
txtResultado.Text = (-1 * int.Parse(txtResultado.Text)).ToString();
}
9.- Botones de limpiar entrada
//CE: CLEAR ENTRY
if(operacion=="")
{
numero1 = 0;
}
else
{
numero2 = 0;
}
txtResultado.Text = "0";
}
10.- Código de limpiar:
private void btnC_Click(object sender, RoutedEventArgs e)
{
//C: CLEAR
numero1 = 0;
numero2 = 0;
operacion = "";
txtResultado.Text = "0";
}
11.- Botón de retroceder:
private void btnRetroceder_Click(object sender, RoutedEventArgs e)
{
if(operacion=="")
{
numero1 = (numero1 / 10);
txtResultado.Text = numero1.ToString();
}
else
{
numero2 = (numero2 / 10);
txtResultado.Text = numero2.ToString();
}
}
12.- Código completo:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace Calculadora
{
/// <summary>
/// Lógica de interacción para MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
long numero1 = 0;
long numero2 = 0;
string operacion = "";
public MainWindow()
{
InitializeComponent();
}
private void btn1_Click(object sender, RoutedEventArgs e)
{
if (operacion == "")
{
numero1 = (numero1 * 10) + 1;
txtResultado.Text = numero1.ToString();
}
else
{
numero2 = (numero2 * 10) + 1;
txtResultado.Text = numero2.ToString();
}
}
private void btn2_Click(object sender, RoutedEventArgs e)
{
if (operacion == "")
{
numero1 = (numero1 * 10) + 2;
txtResultado.Text = numero1.ToString();
}
else
{
numero2 = (numero2 * 10) + 2;
txtResultado.Text = numero2.ToString();
}
}
private void btn3_Click(object sender, RoutedEventArgs e)
{
if (operacion == "")
{
numero1 = (numero1 * 10) + 3;
txtResultado.Text = numero1.ToString();
}
else
{
numero2 = (numero2 * 10) + 3;
txtResultado.Text = numero2.ToString();
}
}
private void btn4_Click(object sender, RoutedEventArgs e)
{
if (operacion == "")
{
numero1 = (numero1 * 10) + 4;
txtResultado.Text = numero1.ToString();
}
else
{
numero2 = (numero2 * 10) + 4;
txtResultado.Text = numero2.ToString();
}
}
private void btn5_Click(object sender, RoutedEventArgs e)
{
if (operacion == "")
{
numero1 = (numero1 * 10) + 5;
txtResultado.Text = numero1.ToString();
}
else
{
numero2 = (numero2 * 10) + 5;
txtResultado.Text = numero2.ToString();
}
}
private void btn6_Click(object sender, RoutedEventArgs e)
{
if (operacion == "")
{
numero1 = (numero1 * 10) + 6;
txtResultado.Text = numero1.ToString();
}
else
{
numero2 = (numero2 * 10) + 6;
txtResultado.Text = numero2.ToString();
}
}
private void btn7_Click(object sender, RoutedEventArgs e)
{
if (operacion == "")
{
numero1 = (numero1 * 10) + 7;
txtResultado.Text = numero1.ToString();
}
else
{
numero2 = (numero2 * 10) + 7;
txtResultado.Text = numero2.ToString();
}
}
private void btn8_Click(object sender, RoutedEventArgs e)
{
if (operacion == "")
{
numero1 = (numero1 * 10) + 8;
txtResultado.Text = numero1.ToString();
}
else
{
numero2 = (numero2 * 10) + 8;
txtResultado.Text = numero2.ToString();
}
}
private void btn9_Click(object sender, RoutedEventArgs e)
{
if (operacion == "")
{
numero1 = (numero1 * 10) + 9;
txtResultado.Text = numero1.ToString();
}
else
{
numero2 = (numero2 * 10) + 9;
txtResultado.Text = numero2.ToString();
}
}
private void btn0_Click(object sender, RoutedEventArgs e)
{
if (operacion == "")
{
numero1 = (numero1 * 10);
txtResultado.Text = numero1.ToString();
}
else
{
numero2 = (numero2 * 10);
txtResultado.Text = numero2.ToString();
}
}
private void btnSumar_Click(object sender, RoutedEventArgs e)
{
operacion = "+";
txtResultado.Text = "0";
}
private void btnRestar_Click(object sender, RoutedEventArgs e)
{
operacion = "-";
txtResultado.Text = "0";
}
private void btnMultiplicar_Click(object sender, RoutedEventArgs e)
{
operacion = "*";
txtResultado.Text = "0";
}
private void btnDividir_Click(object sender, RoutedEventArgs e)
{
operacion = "/";
txtResultado.Text = "0";
}
private void btnResultado_Click(object sender, RoutedEventArgs e)
{
switch(operacion)
{
case "+":
txtResultado.Text = (numero1 + numero2).ToString();
break;
case "-":
txtResultado.Text = (numero1- numero2).ToString();
break;
case "*":
txtResultado.Text = (numero1 * numero2).ToString();
break;
case "/":
txtResultado.Text = (numero1 / numero2).ToString();
break;
}
numero1 = 0;
numero2 = 0;
operacion = "";
}
private void btnCE_Click(object sender, RoutedEventArgs e)
{
//CE: CLEAR ENTRY
if(operacion=="")
{
numero1 = 0;
}
else
{
numero2 = 0;
}
txtResultado.Text = "0";
}
private void btnC_Click(object sender, RoutedEventArgs e)
{
//C: CLEAR
numero1 = 0;
numero2 = 0;
operacion = "";
txtResultado.Text = "0";
}
private void btnRetroceder_Click(object sender, RoutedEventArgs e)
{
if(operacion=="")
{
numero1 = (numero1 / 10);
txtResultado.Text = numero1.ToString();
}
else
{
numero2 = (numero2 / 10);
txtResultado.Text = numero2.ToString();
}
}
private void btnPositivoNegativo_Click(object sender, RoutedEventArgs e)
{
txtResultado.Text = (-1 * int.Parse(txtResultado.Text)).ToString();
// txtResultado.Text = Math.Abs(int.Parse(txtResultado.Text)).ToString();
}
}
}
Ahora solo queda mejorar el código y algunas funciones.Saludos


No hay comentarios:
Publicar un comentario