"Enter" key not behaving as intended when setting AcceptButton Property in WinForms.

Steven Villarreal 21 Reputation points
2021-06-23T14:12:18.847+00:00

I am currently working on creating a basic calculator application that can take in numbers either by pressing the number buttons on screen or pressing the numbers on the keyboard. So far I have gotten pretty much everything done that has to deal with the logic and how everything should work but I cant seem to figure out why the enter key won't work.

I want the "ENTER" key to be tied to the "=" button which would compute the equation I typed into the calculator. Right now when ever I press the "ENTER" key on the keyboard it presses what ever button is focused on the form. If I click the "1" button, the "+" button, and the "2" button and press the "ENTER" key it should compute the equation but instead it presses the "2" button. It is like I need to set the focus to the "=" key in order for the "ENTER" key to work as intended.

  • I have tried several things, I have tried setting the form and its components to not be focusable but that didn't seem to work.
  • I have set the AcceptButton Property of the form in the "Design" window to be set to the equalBtn in the program but that doesn't seem to do anything.
  • I have tried setting the AcceptButton Property of the form in the code but once again that doesn't seem to do a thing.

I have tried just about everything and have scoured stackoverflow for literally a day and haven't been able to find a solution.

Here is my code for the equalBtn,

private void FormBtn_KeyDown(object sender, KeyEventArgs e)
        {

            // NUM PAD 1
            if (e.KeyCode == Keys.D1 || e.KeyCode == Keys.NumPad1)
            {
                oneBtn_Click(sender, e);
            }
            // CHAR NUM PAD 2
            else if (e.KeyCode == Keys.D2 || e.KeyCode == Keys.NumPad2)
            {
                twoBtn_Clicked(sender, e);

            }
            // CHAR NUM PAD 3
            else if (e.KeyCode == Keys.D3 || e.KeyCode == Keys.NumPad3)
            {
                threeBtn_Click(sender, e);

            }

            // CHAR NUM PAD 4
            else if (e.KeyCode == Keys.D4 || e.KeyCode == Keys.NumPad4)
            {
                fourBtn_Click(sender, e);

            }

            // CHAR NUM PAD 5
            else if (e.KeyCode == Keys.D5 || e.KeyCode == Keys.NumPad5)
            {
                fiveBtn_Click(sender, e);

            }

            // CHAR NUM PAD 6
            else if (e.KeyCode == Keys.D6 || e.KeyCode == Keys.NumPad6)
            {
                sixBtn_Click(sender, e);

            }

            // CHAR NUM PAD 7
            else if (e.KeyCode == Keys.D7 || e.KeyCode == Keys.NumPad7)
            {
                sevenBtn_Click(sender, e);

            }

            // CHAR NUM PAD 8
            else if (e.KeyCode == Keys.D8 || e.KeyCode == Keys.NumPad8)
            {
                eightBtn_Click(sender, e);

            }

            // CHAR NUM PAD 9
            else if (e.KeyCode == Keys.D9 || e.KeyCode == Keys.NumPad9)
            {
                nineBtn_Click(sender, e);

            }

            // CHAR NUM PAD 0
            else if (e.KeyCode == Keys.D0 || e.KeyCode == Keys.NumPad0)
            {
                zeroBtn_Click(sender, e);

            }

            // SHIFT + "+"
            else if ((e.Shift) && e.KeyCode == Keys.Oemplus)
            {
                plusBtn_Clicked(sender, e);
            }

            // Subtraction or "-"
            else if (e.KeyCode == Keys.OemMinus)
            {
                minusBtn_Click(sender, e);
            }

            // "/"
            else if (e.KeyCode == Keys.OemQuestion)
            {
                divideBtn_Click(sender, e);
            }

            // Backspace for delete.
            else if (e.KeyCode == System.Windows.Forms.Keys.Back)
            {
                deleteBtn_Click(sender, e);
            }

            // Enter key is pressed so "=" is initiated.
            else if (e.KeyCode == Keys.Enter)
            {
                equalBtn_Click(sender, e);
            }

        }

Like I said I have tried literally everything but for some reason the enter key just always seems to click whatever is focused on the windows form.

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,846 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,350 questions
0 comments No comments
{count} votes

Accepted answer
  1. Karen Payne MVP 35,196 Reputation points
    2021-06-23T14:52:52.147+00:00

    You could use the following

    using System.Diagnostics;
    using System.Windows.Forms;
    
    namespace WindowsFormsApp2
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
            {
                if (keyData != (Keys.Enter) || ActiveControl.Name != "EqualButton") 
                    return base.ProcessCmdKey(ref msg, keyData);
                Debug.WriteLine($"Enter pressed on {ActiveControl.Name}\nDo something now");
                return true;
            }
        }
    }
    
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful