The Enter Key doesn't trigger anything in my form or control

Karim Vazirinejad 186 Reputation points
2021-05-29T09:38:33.837+00:00

I have a calculator app in windows form that work very well when I use mouse for doing operation. I added some code and make it to work with keyboard and keypad. All things work well except ENTER Key. When I do an operation and press the ENTER key, it doesn't trigger anything, but the equal key work fine.
I search a lot and used keypress and keydown events, but it doesn't work and my Enter key doesn't stimulate the equal sign.
the complete code is as follow:

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 Calculator
{
    public partial class frmCalculator : Form
    {
        double x, y, z;
        string op;
        Boolean flag;

        public frmCalculator()
        {
            InitializeComponent();
        }

        private void btnEqual_Click(object sender, EventArgs e)
        {
            y = Convert.ToDouble(txtDisplay.Text);
            switch(op)
            {
                case "+":
                    z = x + y;
                    break;
                case "-":
                    z = x - y;
                    break;
                case "*":
                    z = x * y;
                    break;
                case "/":
                    z = x / y;
                    break;
            }
            txtDisplay.Text = z.ToString();
            op = null;

        }

        private void txtDisplay_TextChanged(object sender, EventArgs e)
        {
            btnPoint.Enabled = !txtDisplay.Text.Contains(".");
            btnBackSpace.Enabled = Convert.ToBoolean(txtDisplay.Text.Length);

        }

        private void btnBackSpace_Click(object sender, EventArgs e)
        {
            if (txtDisplay.TextLength>0)
            txtDisplay.Text = txtDisplay.Text.Substring(0, txtDisplay.Text.Length - 1);
        }

        private void frmCalculator_Load(object sender, EventArgs e)
        {
            txtDisplay_TextChanged(null, null);
        }

        private void frmCalculator_KeyPress(object sender, KeyPressEventArgs e)
        {
            foreach (Button x in panelNumbers.Controls)
                if (x.Text == e.KeyChar.ToString())
                {
                    x.Focus();
                    x.ForeColor = Color.Red;
                }
                else
                    x.ForeColor = Color.Black;


            Button temp = new Button();
           temp.Text = e.KeyChar.ToString();
            if (e.KeyChar >= '0' && e.KeyChar <= '9')
                Numbers(temp, null);
            else if (e.KeyChar == '+' || e.KeyChar == '-' || e.KeyChar == '*' || e.KeyChar == '/')
                Operators(temp, null);
            else if (e.KeyChar == '=')
                btnEqual_Click(null, null);
            else if (e.KeyChar == '.' && txtDisplay.Text.Contains(".") == false)
                Numbers(temp, null);
            else if (e.KeyChar == '\b')
                btnBackSpace_Click(null, null);

        }

        private void frmCalculator_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
                btnEqual_Click(null, null);
        }


        private void Operators(object sender, EventArgs e)
        {
            if (op != null)
                btnEqual_Click(null, null);
            x = Convert.ToDouble(txtDisplay.Text);
            op = ((Button)sender).Text;
            flag = true;
        }



        private void Numbers(object sender, EventArgs e)
        {

            if (flag==true)
            {
                txtDisplay.Text = "";
                flag = false;
            }
            txtDisplay.Text += ((Button)sender).Text;
        }
    }
}
Developer technologies | C#
0 comments No comments
{count} votes

Accepted answer
  1. Timon Yang-MSFT 9,606 Reputation points
    2021-05-31T06:38:03.41+00:00

    Is it possible that you did not bind the frmCalculator_KeyDown event to the form, causing the following codes to fail to execute:

             private void frmCalculator_KeyDown(object sender, KeyEventArgs e)  
             {  
                 if (e.KeyData == Keys.Enter)  
                     btnEqual_Click(null, null);  
             }  
    

    Try adding a breakpoint in the method to check it.

    In addition, why not move the code in this method to the frmCalculator_KeyPress method?

    Since you said that all the other keys are useful, there must be no problem with this method.


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Anonymous
    2021-05-29T11:50:06.99+00:00

    Inside of the form load event try adding this statement:

    This.KeyPreview = True:
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.