Alright guys, I'm a senior in high school, and as it says in the title I need to help me in the realization of the basic calculator (which I already have functional with the mouse), but now I need to adapt it for that accept me values by keyboard (you know, numbers, operations and the delete button, and that when I hit enter, give me the result) thing with which I have had many problems.
The numbers already enter me, but not the other mentioned, I hope they help me is urgent, I am a rookie and it is the first time I do something like that.
Here is the code so you can check it out, it's all commented, thank you very much in advance.
public partial class Form1 : Form
{
bool detectaopreacion = true;
string operacion;
double result;
double numero1;
double numero2;
bool punto = true;
string borrado, point;
double Signos;
double memoria;
bool labelmemory = false
//Instrucciones para botones numericos
private void btn0_Click(object sender, EventArgs e)
{
if (txtrespuesta.Text == "0")
{
return;
}
else
{
txtrespuesta.Text = txtrespuesta.Text + "0";
}
}
private void btn1_Click(object sender, EventArgs e)
{
if (detectaopreacion)
{
txtrespuesta.Text = "";
txtrespuesta.Text = "1";
detectaopreacion = false;
}
else
{
txtrespuesta.Text = txtrespuesta.Text + "1";
}
}
private void btn2_Click(object sender, EventArgs e)
{
if (detectaopreacion)
{
txtrespuesta.Text = "";
txtrespuesta.Text = "2";
detectaopreacion = false;
}
else
{
txtrespuesta.Text = txtrespuesta.Text + "2";
}
}
private void btn3_Click(object sender, EventArgs e)
{
if (detectaopreacion)
{
txtrespuesta.Text = "";
txtrespuesta.Text = "3";
detectaopreacion = false;
}
else
{
txtrespuesta.Text = txtrespuesta.Text + "3";
}
}
private void btn4_Click(object sender, EventArgs e)
{
if (detectaopreacion)
{
txtrespuesta.Text = "";
txtrespuesta.Text = "4";
detectaopreacion = false;
}
else
{
txtrespuesta.Text = txtrespuesta.Text + "4";
}
}
private void btn5_Click(object sender, EventArgs e)
{
if (detectaopreacion)
{
txtrespuesta.Text = "";
txtrespuesta.Text = "5";
detectaopreacion = false;
}
else
{
txtrespuesta.Text = txtrespuesta.Text + "5";
}
}
private void btn6_Click(object sender, EventArgs e)
{
if (detectaopreacion)
{
txtrespuesta.Text = "";
txtrespuesta.Text = "6";
detectaopreacion = false;
}
else
{
txtrespuesta.Text = txtrespuesta.Text + "6";
}
}
private void btn7_Click(object sender, EventArgs e)
{
if (detectaopreacion)
{
txtrespuesta.Text = "";
txtrespuesta.Text = "7";
detectaopreacion = false;
}
else
{
txtrespuesta.Text = txtrespuesta.Text + "7";
}
}
private void btn8_Click(object sender, EventArgs e)
{
if (detectaopreacion)
{
txtrespuesta.Text = "";
txtrespuesta.Text = "8";
detectaopreacion = false;
}
else
{
txtrespuesta.Text = txtrespuesta.Text + "8";
}
}
private void btn9_Click(object sender, EventArgs e)
{
if (detectaopreacion)
{
txtrespuesta.Text = "";
txtrespuesta.Text = "9";
detectaopreacion = false;
}
else
{
txtrespuesta.Text = txtrespuesta.Text + "9";
}
}
//Fin de asignación númerica
//Botones de operaciones, asignación
//suma
private void btnmas_Click(object sender, EventArgs e)
{
operacion = "+";
detectaopreacion = true;
numero1 = double.Parse(txtrespuesta.Text);
}
//resta
private void btnmenos_Click(object sender, EventArgs e)
{
operacion = "-";
detectaopreacion = true;
numero1 = double.Parse(txtrespuesta.Text);
}
//Multiplicación
private void button12_Click(object sender, EventArgs e)
{
operacion = "*";
detectaopreacion = true;
numero1 = double.Parse(txtrespuesta.Text);
}
//División
private void btndividir_Click(object sender, EventArgs e)
{
operacion = "/";
detectaopreacion = true;
numero1 = double.Parse(txtrespuesta.Text);
}
//Raiz Cuadrada
private void btnraiz_Click(object sender, EventArgs e)
{
numero1 = double.Parse(txtrespuesta.Text);
result = Math.Sqrt(numero1);
txtrespuesta.Text = result.ToString();
detectaopreacion = true;
}
//Potencia
private void cuadrado_Click(object sender, EventArgs e)
{
numero1 = double.Parse(txtrespuesta.Text);
result = numero1 * numero1;
txtrespuesta.Text = result.ToString();
}
//Porcentaje
private void btnporcentaje_Click(object sender, EventArgs e)
{
numero1 = double.Parse(txtrespuesta.Text);
result = ((numero1 * numero2) / 100);
txtrespuesta.Text = result.ToString();
detectaopreacion = true;
}
//Fin de operaciones
//Botón de igualdad, y llamado de operaciones
private void btnigual_Click(object sender, EventArgs e)
{
numero2 = double.Parse(txtrespuesta.Text);
detectaopreacion = true;
switch (operacion)
{
case "+":
result = numero1 + numero2;
txtrespuesta.Text = result.ToString();
break;
case "-":
result = numero1 - numero2;
txtrespuesta.Text = result.ToString();
break;
case "*":
result = numero1 * numero2;
txtrespuesta.Text = result.ToString();
break;
case "/":
result = numero1 / numero2;
txtrespuesta.Text = result.ToString();
break;
}
}
//Boton Igual
private void btnpunto_Click(object sender, EventArgs e)
{
if (punto == true)
{
txtrespuesta.Text = txtrespuesta.Text + ".";
punto = false;
}
else
{
return;
}
detectaopreacion = false;
}
//Botón limpiar todo
private void btnce_Click(object sender, EventArgs e)
{
txtrespuesta.Text = "0";
numero1 = 0;
numero2 = 0;
detectaopreacion = true;
punto = true;
}
//Botón limpiar
private void btnc_Click(object sender, EventArgs e)
{
txtrespuesta.Text = "0";
detectaopreacion = true;
numero1 = 0;
numero2 = 0;
result = 0;
}
//Boton retroceso/eliminado unitario
private void btnretroceso_Click(object sender, EventArgs e)
{
int x = 0;
int y = 0;
borrado = txtrespuesta.Text;
point = txtrespuesta.Text;
x = borrado.Length - 1;
y = point.Length - 1;
point = point.Substring(y, 1);
borrado = borrado.Substring(0, x);
txtrespuesta.Text = borrado;
if (txtrespuesta.Text == "")
{
txtrespuesta.Text = "0";
detectaopreacion = true;
}
if (txtrespuesta.Text == "-")
{
txtrespuesta.Text = "0";
detectaopreacion = true;
}
if (point == ".")
{
punto = true;
}
}
//Boton memory clear(borrar memoria)
private void btnmc_Click(object sender, EventArgs e)
{
memoria = 0;
labelmemory = false;
}
//Boton more memory (Más memoria)
private void btnmr_Click(object sender, EventArgs e)
{
txtrespuesta.Text = memoria.ToString();
}
//Boton MS(no me acuerdo de las siglas, zorry :p)
private void btnms_Click(object sender, EventArgs e)
{
memoria = Convert.ToDouble(txtrespuesta.Text);
labelmemory = true;
}
//Boton memoria +
private void btnmmas_Click(object sender, EventArgs e)
{
memoria = memoria + Convert.ToDouble(txtrespuesta.Text);
labelmemory = true;
}
//Boton memoria -
private void btnmmenos_Click(object sender, EventArgs e)
{
memoria = memoria - Convert.ToDouble(txtrespuesta.Text);
labelmemory = true;
}
//Boton +-
private void btnmasmenos_Click(object sender, EventArgs e)
{
Signos = double.Parse(txtrespuesta.Text);
Signos = Signos - (Signos * 2);
txtrespuesta.Text = Signos.ToString();
}
//evento de validacion para admitir numeros por teclado
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
switch(e.KeyChar.ToString())
{
case "0":
btn0.PerformClick();
break;
case "1":
btn1.PerformClick();
break;
case "2":
btn2.PerformClick();
break;
case "3":
btn3.PerformClick();
break;
case "4":
btn4.PerformClick();
break;
case "5":
btn5.PerformClick();
break;
case "6":
btn6.PerformClick();
break;
case "7":
btn7.PerformClick();
break;
case "8":
btn8.PerformClick();
break;
case "9":
btn9.PerformClick();
break;
case "+":
btnmas_Click();
break;
case "-":
btnmenos.PerformClick();
break;
case "*":
btnmul.PerformClick();
break;
case "/":
btndividir.PerformClick();
break;
case "s":
cuadrado.PerformClick();
break;
case "Enter":
btnigual_Click();
break;
}
}
private void btnigual_Click()
{
btnigual.PerformClick();
numero2 = double.Parse(txtrespuesta.Text);
detectaopreacion = true;
switch (operacion)
{
case "+":
result = numero1 + numero2;
txtrespuesta.Text = result.ToString();
break;
case "-":
result = numero1 - numero2;
txtrespuesta.Text = result.ToString();
break;
case "*":
result = numero1 * numero2;
txtrespuesta.Text = result.ToString();
break;
case "/":
result = numero1 / numero2;
txtrespuesta.Text = result.ToString();
break;
}
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void fuelEconomiToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void editarToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void cientificaToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Hide();
Form Form2 = new Form2();
Form2.Show();
}
private void btnmas_Click()
{
detectaopreacion = true;
numero2 = double.Parse(txtrespuesta.Text);
}
}
}