4

Estava desenvolvendo uma calculadora em C#, e comecei a fazer a parte de ativação dos botões através do teclado.

if (e.KeyCode == Keys.Add) 
  {
    btnsum.PerformClick();
  } 

Utilizando o código acima, já consegui fazer a maior parte da ativação dos botões, mas então surgiu minha dúvida.

Existe uma maneira de fazer a ativação do botão % (porcentagem) ao pressionar o Shift, juntamente com o 5?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Calculadora
{
    public partial class frmCalculadora : Form
    {
        public frmCalculadora()
        {
            InitializeComponent();
        }

        public string n1 = "", n2 = "";
        double memory = 0;
        public bool estado = false;

        class Calc
        {
            public string Op = "";

            public string Igual(string strN1, string strN2)
            {
                double n1 = Convert.ToDouble(strN1);
                double n2 = Convert.ToDouble(strN2);
                double resul = 0;

                switch (Op)
                {
                    case "/":
                        resul = n1 / n2;
                        break;
                    case "*":
                        resul = n1 * n2;
                        break;
                    case "-":
                        resul = n1 - n2;
                        break;
                    case "+":
                        resul = n1 + n2;
                        break;
                    case "mod":
                        resul = n1 % n2;
                        break;
                    case "log":
                        resul = Math.Pow(n1, n2);
                        break;
                }
                return Convert.ToString(resul);
            }
        }
        Calc c = new Calc();

        private void btn0_Click(object sender, EventArgs e)
        {
            if (estado)
            {
                txtVisor.Text = "0";
                estado = false;
            }
            else
            {
                txtVisor.Text += "0"; 
            }
        }

        private void btn1_Click(object sender, EventArgs e)
        {
            if (estado)
            {
                txtVisor.Text = "1";
                estado = false;
            }
            else
            {
                txtVisor.Text += "1";
            }
        }

        private void btn2_Click(object sender, EventArgs e)
        {
            if (estado)
            {
                txtVisor.Text = "2";
                estado = false;
            }
            else
            {
                txtVisor.Text += "2";
            }
        }

        private void btn3_Click(object sender, EventArgs e)
        {
            if (estado)
            {
                txtVisor.Text = "3";
                estado = false;
            }
            else
            {
                txtVisor.Text += "3";
            }
        }

        private void btn4_Click(object sender, EventArgs e)
        {
            if (estado)
            {
                txtVisor.Text = "4";
                estado = false;
            }
            else
            {
                txtVisor.Text += "4";
            }
        } 

        private void btn5_Click(object sender, EventArgs e)
        {
            if (estado)
            {
                txtVisor.Text = "5";
                estado = false;
            }
            else
            {
                txtVisor.Text += "5";
            }
        }

        private void btn6_Click(object sender, EventArgs e)
        {
            if (estado)
            {
                txtVisor.Text = "6";
                estado = false;
            }
            else
            {
                txtVisor.Text += "6";
            }
        }

        private void btn7_Click(object sender, EventArgs e)
        {
            if (estado)
            {
                txtVisor.Text = "7";
                estado = false;
            }
            else
            {
                txtVisor.Text += "7";
            }
        }

        private void btn8_Click(object sender, EventArgs e)
        {
            if (estado)
            {
                txtVisor.Text = "8";
                estado = false;
            }
            else
            {
                txtVisor.Text += "8";
            }
        }

        private void btn9_Click(object sender, EventArgs e)
        {
            if (estado)
            {
                txtVisor.Text = "9";
                estado = false;
            }
            else
            {
                txtVisor.Text += "9";
            }
        }

        private void btnsum_Click(object sender, EventArgs e)
        {
            try{
            n1 = txtVisor.Text;
            c.Op = "+";
            estado = true;
            }
            catch 
            {
                return;
            }
        }

        private void btnmin_Click(object sender, EventArgs e)
        {
            try{
            n1 = txtVisor.Text;
            c.Op = "-";
            estado = true;
            }
            catch 
            {
                return;
            }
        }

        private void btnmult_Click(object sender, EventArgs e)
        {
            try{
            n1 = txtVisor.Text;
            c.Op = "*";
            estado = true;
            }
            catch 
            {
                return;
            }
        }

        private void btndiv_Click(object sender, EventArgs e)
        {
            try{
            n1 = txtVisor.Text;
            c.Op = "/";
            estado = true;
            }
            catch 
            {
                return;
            }
        }

        private void btnraiz_Click(object sender, EventArgs e)
        {
            try{
            n1 = txtVisor.Text;
            double doubleN1 = Convert.ToDouble(n1);

            double result = Math.Sqrt(doubleN1);
            txtVisor.Text = Convert.ToString(result);
            }
            catch 
            {
                return;
            }
        }

       private void btnporc_Click(object sender, EventArgs e)
        {
           try{
            double N1 = Convert.ToDouble(n1); 
            double N2 = Convert.ToDouble(txtVisor.Text);
            txtVisor.Text = Convert.ToString(N1 * N2 /100);
           }
           catch 
           {
               return;
           }
        }

        private void btnigu_Click(object sender, EventArgs e)
        {
            try{
            n2 = txtVisor.Text;
            txtVisor.Text = c.Igual(n1, n2);
            }
            catch 
            {
                return;
            }
        }

        private void btnmod_Click(object sender, EventArgs e)
        {
            try{
            n1 = txtVisor.Text;
            c.Op = "mod";
            estado = true;
            }
            catch 
            {
                return;
            }
        }

        private void btnlog_Click(object sender, EventArgs e)
        {
            try{
            n1 = txtVisor.Text;
            c.Op = "log";
            estado = true;
            }
            catch 
            {
                return;
            }
        }

        private void btnvirgula_Click(object sender, EventArgs e)
        {
            try{
            int virg;
            virg = txtVisor.Text.IndexOf(",");   
            if (virg >= 0)                    
                return;
            else                           
            txtVisor.Text += ",";
            }
            catch 
            {
                return;
            }
        }

        private void btnclear_Click(object sender, EventArgs e)
        {
            txtVisor.Text = "";
            n1 = "0";
            n2 = "0";
            c.Op = "0";
        }

        private void btnback_Click(object sender, EventArgs e)
        {
            try{
            char[] arr = txtVisor.Text.ToCharArray();

            string txt = "";

            int tamanho = arr.Length;

            for (int i = 0; i < arr.Length; i++)
            {

                if (i + 1 != tamanho)
                {

                    txt += arr[i].ToString();

                }

            }

            txtVisor.Text = txt;
            }
            catch 
            {
                return;
            }

        }

        private void btnpot_Click(object sender, EventArgs e)
        {
            try{
            double N1 = Convert.ToDouble(txtVisor.Text);
            txtVisor.Text = Convert.ToString((N1 * N1) * N1);
            }
            catch 
            {
                return;
            }
        }

        private void btn_inverter_Click(object sender, EventArgs e)
        {
            try{
            double n1 = double.Parse(txtVisor.Text) * (-1);
            txtVisor.Text = n1.ToString();
            }
            catch 
            {
                return;
            }
        }

        private void btn_quadrado_Click(object sender, EventArgs e)
        {
            try{
            double N1 = Convert.ToDouble(txtVisor.Text);
            txtVisor.Text = Convert.ToString(N1 * N1);
            }
            catch 
            {
                return;
            }
        }

        private void btn_inv_Click(object sender, EventArgs e)
        {
            try
            {
                double d = double.Parse(txtVisor.Text);
                if (d == 0)
                {
                    return;
                }
                else
                {
                    d = 1 / d;
                    txtVisor.Text = d.ToString();
                }
            }
            catch
            {
                    return;
            }
        }

        private void btnCE_Click(object sender, EventArgs e)
        {
            try{
            txtVisor.Text = "";
            }
            catch 
            {
                return;
            }
        }

        private void btnmmais_Click(object sender, EventArgs e)
        {
            try{
            memory += Convert.ToDouble(txtVisor.Text);
            txtVisor.Clear();
            }
            catch 
            {
                return;
            }
        }

        private void btnmclear_Click(object sender, EventArgs e)
        {
            try{
            memory = Convert.ToDouble("0");
            txtVisor.Clear();
            }
            catch 
            {
                return;
            }
        }

        private void btnmmenos_Click(object sender, EventArgs e)
        {
            try{
            memory -= Convert.ToDouble(txtVisor.Text);
            txtVisor.Clear();

            }catch{
                return;
            }
        }

        private void btnmresult_Click(object sender, EventArgs e)
        {
            try{
            txtVisor.Text = Convert.ToString(memory);
            }
            catch
            {
                return;
            }
        }

        private void btnmsave_Click(object sender, EventArgs e)
        {
            try{
            memory = Convert.ToDouble(txtVisor.Text);
            txtVisor.Clear();
            }
            catch
            {
                return;
            }
        }

        private void txtVisor_TextChanged(object sender, EventArgs e)
        {

        }

        private void Form1_KeyPress(object sender, KeyEventArgs e)
        { 
            if (e.KeyCode == Keys.Add) 
            {
                btnsum.PerformClick();
            }
            else if (e.KeyCode == Keys.Subtract)
            {
                btnmin.PerformClick();
            }
            else if (e.KeyCode == Keys.Multiply)
            {
                btnmult.PerformClick();
            }
            else if (e.KeyCode == Keys.Divide)
            {
                btndiv.PerformClick();
            }
            else if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Separator)
            {
                btnigu.PerformClick();
            }
            else if (e.KeyCode == Keys.D0 || e.KeyCode == Keys.NumPad0)
            {
                btn0.PerformClick();
            }
            else if (e.KeyCode == Keys.D1 || e.KeyCode == Keys.NumPad1)
            {
                btn1.PerformClick();

            }
            else if (e.KeyCode == Keys.D2 || e.KeyCode == Keys.NumPad2)
            {
                btn2.PerformClick();

            }
            else if (e.KeyCode == Keys.D3 || e.KeyCode == Keys.NumPad3)
            {
                btn3.PerformClick();

            }
            else if (e.KeyCode == Keys.D4 || e.KeyCode == Keys.NumPad4)
            {
                btn4.PerformClick();

            }
            else if (e.KeyCode == Keys.D5 || e.KeyCode == Keys.NumPad5)
            {
                btn5.PerformClick();

            }
            else if (e.KeyCode == Keys.D6 || e.KeyCode == Keys.NumPad6)
            {
                btn6.PerformClick();

            }
            else if (e.KeyCode == Keys.D7 || e.KeyCode == Keys.NumPad7)
            {
                btn7.PerformClick();
            }
            else if (e.KeyCode == Keys.D8 || e.KeyCode == Keys.NumPad8)
            {
                btn8.PerformClick();

            }
            else if (e.KeyCode == Keys.D9 || e.KeyCode == Keys.NumPad9)
            {
                btn9.PerformClick();

            }
            else if (e.KeyCode == Keys.Decimal || e.KeyCode == Keys.OemPeriod || e.KeyCode == Keys.Oemcomma || e.KeyCode == Keys.None)
            {
                btnvirgula.PerformClick();

            }
            else if (e.KeyCode == Keys.Back)
            {
                btnback.PerformClick();

            }
            else if (e.KeyCode == Keys.Delete)
            {
                btnclear.PerformClick();

            }
            else if (e.KeyCode == Keys.Delete)
            {
                btnback.PerformClick();

            }
            else if (e.Modifiers == Keys.Shift && e.KeyCode == Keys.D5)
            {
                btnporc.PerformClick();
            }


        }

    }

}
1
  • 1
    Poste mais parte do seu código para entendermos melhor o escopo. O que postei saberá quando o shift e o 5 forem pressionados. 24/04/2017 às 18:26

1 Resposta 1

3

Você terá que combinar as propriedades KeyCode e Modifiers para formar o "%":

if (e.Modifiers == Keys.Shift && e.KeyCode == Keys.D5)
    //seu código

ou acessar diretamente a propriedade Shift:

if (e.Shift && e.KeyCode == Keys.D5)
    //seu código

Atualização da resposta

Para realizar a operação de porcentagem sem que essa tecla seja inserida no textbox, você terá que utilizar o evento KeyPress.
O evento KeyDown, o que você usou, é após a tecla já ter sido pressionada, com isso, teria que fazer umas POG para contornar isso - desnecessário.

Veja como deve ficar ao meu ver:

private void frmCalculadora_KeyPress(object sender, KeyPressEventArgs e) {
    if (e.KeyChar.ToString() == "%") {
        btnPercent.PerformClick();
        e.Handled = true;
    }
}

Para evitar que o caractere seja inserido no texto, setamos para true a propriedade Handled após chamar o evento click.

0

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .