Inquiridor
Ajuda com Calculadora

Pergunta
-
Boa noite,
A um tempo atraz fiz uma calculadora em c# e hoje estou tentando refaze-la para windows phone, porem existe umas propertis do c# que não tem no windows phone, como é o caso da KeyPreview... queria uma ajuda de vocês para que eu possa termina-la.
Agradeço desde já.
[code]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
namespace Calculadora
{
public partial class MainPage : PhoneApplicationPage
{
//declaração variaveis
Boolean Limpaexibicao;
double Operando1 = 0, Operando2 = 0;
string Operador;
// Metodo Construtor
public MainPage()
{
InitializeComponent();
}
//incio comando botão numeros
private void btn1_Click(object sender, RoutedEventArgs e)
{
if (Limpaexibicao)
{
txtDisplay.Text = "";
Limpaexibicao = false;
}
Button Botao = (Button)sender;
if (Botao.Name.Equals("btn0"))
{
if (txtDisplay.Text.Equals("0"))
{
}
else
{
txtDisplay.Text = txtDisplay.Text + Botao.Content;
}
}
else
{
if (txtDisplay.Text.Equals("0"))
{
txtDisplay.Text += Botao;
}
else
{
txtDisplay.Text = txtDisplay.Text + Botao.Content;
}
}
}
private void stackPanel1(object sender, EventArgs e)
{
this.KeyPreview = true;
btn0.Click += btn1_Click;
btn2.Click += btn1_Click;
btn3.Click += btn1_Click;
btn4.Click += btn1_Click;
btn5.Click += btn1_Click;
btn6.Click += btn1_Click;
btn7.Click += btn1_Click;
btn8.Click += btn1_Click;
btn9.Click += btn1_Click;
}
//fim comando botões numeros
//incio botão limpar C
private void btnlimpar_Click(object sender, RoutedEventArgs e)
{
txtDisplay.Text="0";
txtDisplay.Focus();
Operando1 = 0;
Operando2 = 0;
Operador = "";
}
//fim botão limpar C
//inicio botão soma
private void btnSomar_Click(object sender, RoutedEventArgs e)
{
if (Operando1 != 0)
{
Operando2 = Double.Parse(txtDisplay.Text);
}
else
{
Operando1 = double.Parse(txtDisplay.Text);
}
Operador = "+";
if ((Operando1 != 0) && (Operando2 != 0))
{
btnIgual_Click(null, null);
}
Limpaexibicao = true;
}
//fim botão soma
//inicio comando botão igual
private void btnIgual_Click(object sender, RoutedEventArgs e)
{
double Resultado = 0;
Operando2 = double.Parse(txtDisplay.Text);
if (Operador.Equals("+"))
{
Resultado = Operando1 + Operando2;
}
else if (Operador.Equals("-"))
{
Resultado = Operando1 - Operando2;
}
else if (Operador.Equals("*"))
{
Resultado = Operando1 * Operando2;
}
else if (Operador.Equals("/"))
{
if (Operando2 != 0)
{
Resultado = Operando1 / Operando2;
}
else
{
txtDisplay.Text = "Impossivel dividir por 0";
Limpaexibicao = true;
return;
}
}
Operando1 = Resultado;
txtDisplay.Text = Resultado.ToString();
Limpaexibicao = true;
}
//fim comando botão igual
//comando operação
private void txtDisplay_TextChanged(object sender, TextChangedEventArgs e)
{
}
private void btnSubtrair_Click(object sender, RoutedEventArgs e)
{
if (Operando1 != 0)
{
Operando2 = Double.Parse(txtDisplay.Text);
}
else
{
Operando1 = double.Parse(txtDisplay.Text);
}
Operador = "-";
if ((Operando1 != 0) && (Operando2 != 0))
{
btnIgual_Click(null, null);
}
Limpaexibicao = true;
}
private void btnMultiplicar_Click(object sender, RoutedEventArgs e)
{
if (Operando1 != 0)
{
Operando2 = Double.Parse(txtDisplay.Text);
}
else
{
Operando1 = double.Parse(txtDisplay.Text);
}
Operador = "*";
if ((Operando1 != 0) && (Operando2 != 0))
{
btnIgual_Click(null, null);
}
Limpaexibicao = true;
}
private void btnDividir_Click(object sender, RoutedEventArgs e)
{
if (Operando1 != 0)
{
Operando2 = Double.Parse(txtDisplay.Text);
}
else
{
Operando1 = double.Parse(txtDisplay.Text);
}
Operador = "/";
if ((Operando1 != 0) && (Operando2 != 0))
{
btnIgual_Click(null, null);
}
Limpaexibicao = true;
}
private void btnPonto_Click(object sender, RoutedEventArgs e)
{
if (txtDisplay.Text.IndexOf (",") > 0)
{
}
else if (txtDisplay.Text.Length==0)
{
txtDisplay.Text= "0,";
}
else
{
txtDisplay.Text = txtDisplay.Text + (",");
}
}
private void ContentPanel_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.NumPad0)//se o contedudo da tecla for = a numerico 0 temos que chamar o botão zero
{
btn1_Click(btn0, null);
}
else if (e.Key == Key.NumPad1)
{
btn1_Click(btn1, null);
}
else if (e.Key == Key.NumPad2)
{
btn1_Click(btn2, null);
}
else if (e.Key == Key.NumPad3)
{
btn1_Click(btn3, null);
}
else if (e.Key == Key.NumPad4)
{
btn1_Click(btn4, null);
}
else if (e.Key == Key.NumPad5)
{
btn1_Click(btn5, null);
}
else if (e.Key == Key.NumPad6)
{
btn1_Click(btn6, null);
}
else if (e.Key == Key.NumPad6)
{
btn1_Click(btn6 ,null);
}
else if (e.Key == Key.NumPad7)
{
btn1_Click(btn7, null);
}
else if (e.Key == Key.NumPad8)
{
btn1_Click(btn8, null);
}
else if (e.Key == Key.NumPad9)
{
btn1_Click(btn9, null);
}
else if (e.Key == Key.Decimal)
{
btnPonto_Click(btnPonto, null);
}
else if (e.Key == Key.Add)
{
btnSomar_Click (btnSomar,null);
}
else if (e.Key == Key.Subtract)
{
btnSubtrair_Click(btnSubtrair, null);
}
else if (e.Key == Key.Divide)
{
btnDividir_Click(btnDividir, null);
}
else if (e.Key == Key.Multiply)
{
btnMultiplicar_Click(btnMultiplicar, null);
}
else if (e.Key == Key.Delete)
{
btnIgual_Click(btnIgual, null);
}
}
private void btn0_Click(object sender, RoutedEventArgs e)
{
}
}
}[/code]
- Movido Felipo Gonçalves segunda-feira, 24 de junho de 2013 12:36
Todas as Respostas
-
-
Boa tarde Paulo,
eu preciso fazer uma calculadora, e preciso fazer o botão decimal, onde se eu clicar no 0(zero) ele não adicione no visor se não for utilizado o .(ponto)
ex: 0.50
O evento utilizado no c# kwyPress faz o seguinte neste projeto:
redireciona o evento, quando alguém clicar em algum desses botoes vai disparar o evento do botão 1.
Este projeto eu fiz ele no C# e funciona legal, fui tentar montalo pra windows phone e não deu certo...
Agradeço a ajuda.
-