-1
Hi, guys, I’m new to C#. Well, for teaching purposes, I’m developing this calculator that you see in the image. It is almost ready, however, it only lacks the function of when I type a number of the keyboard, the same appears in the textbox of the calculator. How do I do this? On the net I only find examples to press a button and show a Messagebox, I want to type one on the keyboard and appear 1 on the calculator.
Follows the code - imported from Github:
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
{
public partial class Calculadora : Form
{
string operador;
Double a = 0;
bool validar;
public Calculadora()
{
InitializeComponent();
}
private void button_Click(object sender, EventArgs e)
{
Button button = (Button)sender;
Tela.Text = Tela.Text + button.Text;
}
private void button_Click_CE(object sender, EventArgs e)
{
Tela.Text = "";
}
private void button_Click_Adc(object sender, EventArgs e)
{
if (validar == true)
{
a = a + Convert.ToDouble(Tela.Text);
label1.Text = Convert.ToString(a) + "+";
Tela.Text = "";
operador = "+";
}
else
{
label1.Text = Tela.Text + buttonAdc.Text;
a = Convert.ToDouble(Tela.Text);
Tela.Text = "";
operador = "+";
validar = true;
}
}
private void button_Click_Sub(object sender, EventArgs e)
{
if (validar == true)
{
a = a - Convert.ToDouble(Tela.Text);
label1.Text = Convert.ToString(a) + "+";
Tela.Text = "";
operador = "-";
}
else
{
label1.Text = Tela.Text + buttonSub.Text;
a = Convert.ToDouble(Tela.Text);
Tela.Text = "";
operador = "-";
validar = true;
}
}
private void button_Click_Multi(object sender, EventArgs e)
{
if (validar == true)
{
a = a * Convert.ToDouble(Tela.Text);
label1.Text = Convert.ToString(a) + "*";
Tela.Text = "";
operador = "*";
}
else
{
label1.Text = Tela.Text + buttonMulti.Text;
a = Convert.ToDouble(Tela.Text);
Tela.Text = "";
operador = "*";
validar = true;
}
}
private void button_Click_Div(object sender, EventArgs e)
{
if (validar == true)
{
a = a / Convert.ToDouble(Tela.Text);
label1.Text = Convert.ToString(a) + "/";
Tela.Text = "";
operador = "/";
}
else
{
label1.Text = Tela.Text + buttonDiv.Text;
a = Convert.ToDouble(Tela.Text);
Tela.Text = "";
operador = "/";
validar = true;
}
}
private void button_Click_C(object sender, EventArgs e)
{
Tela.Text = "";
label1.Text = "";
a = 0;
validar = false;
}
private void button_Click_Igual(object sender, EventArgs e)
{
if (operador == "+")
{
label1.Text = label1.Text + Tela.Text + "=";
Tela.Text = Convert.ToString(a + Convert.ToInt32(Tela.Text));
}
else if (operador == "-")
{
label1.Text = label1.Text + Tela.Text + "=";
Tela.Text = Convert.ToString(a - Convert.ToSingle(Tela.Text));
}
else if (operador == "*")
{
label1.Text = label1.Text + Tela.Text + "=";
Tela.Text = Convert.ToString(a * Convert.ToSingle(Tela.Text));
}
else if (operador == "/")
{
label1.Text = label1.Text + Tela.Text + "=";
Tela.Text = Convert.ToString(a / Convert.ToSingle(Tela.Text));
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
Do not put the code in external file leave it straight in question.
– Augusto Vasques