Keyboard events in C# in textbox

Asked

Viewed 190 times

-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)
        {

        }        

    }
}

inserir a descrição da imagem aqui

  • Do not put the code in external file leave it straight in question.

2 answers

2

Change the property Keypreview from form to true

And in the Form Keypress event:

private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
    // Precisa fazer para aceitar apenas caracteres validos
    Tela.Text += e.KeyChar;

    e.Handled = true;
}

2

First set the property KeyPreview from the form to true so that keyboard messages are received by the form before they reach the controls in the form.

Place an event OnKeyPress() in the Form, use the property KeyPressEventArgs.KeyChar to map the pressed key and adjust the property KeyPressEventArgs.Handled astrue to inform the system event handler that you know the form has solved the event and does not extend it to the other components.

The code will look like this:

void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
                switch (e.KeyChar)
                {
                    case (char)48: Tela.Text = Tela.Text + "0"; break;
                    case (char)49: Tela.Text = Tela.Text + "1"; break;
                    case (char)50: Tela.Text = Tela.Text + "2"; break;
                          // .
                          // .     Os caracteres ASCII de 48 até 57 representam os números de 0 a 9. 
                          // .     Para evitar repetições no exemplo usei as reticências.
                          // .
                    case (char)57: Tela.Text = Tela.Text + "9"; break;

                    // Manipula o `+`
                    case (char)43: button_Click_Adc(this, EventArgs.Empty); break;
                    // Manipula o `-`
                    case (char)45: button_Click_Sub(this, EventArgs.Empty); break;
                    // Manipula o `*`
                    case (char)42: button_Click_Multi(this, EventArgs.Empty); break;
                    // Manipula o `/`
                    case (char)47: button_Click_Div(this, EventArgs.Empty); break;
                    // Manipula o `=`
                    case (char)61: button_Click_Igual(this, EventArgs.Empty); break;

                }
                e.Handled = true;

}
  • 1

    That’s right Augusto. Thanks a lot, bro!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.