question

StevenVillarreal-9519 avatar image
0 Votes"
StevenVillarreal-9519 asked karenpayneoregon commented

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

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.

dotnet-csharpwindows-forms
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

karenpayneoregon avatar image
1 Vote"
karenpayneoregon answered karenpayneoregon commented

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;
         }
     }
 }
· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Now how would I even implement this into my code? The method I posted above in my original posting is how most of the key event handlers are handled and made use of. For the method you shared how would I call it? Like would I have to call it inside where line 96 is?

0 Votes 0 ·

Read the documentation. You simply add the code to your form.

This method is called during message preprocessing to handle command keys. Command keys are keys that always take precedence over regular input keys. Examples of command keys include accelerators and menu shortcuts. The method must return true to indicate that it has processed the command key, or false to indicate that the key is not a command key.


0 Votes 0 ·

AHH thank you! After doing some debugging to see what ProcessCmdKey does I was finally able to get it to work! Thank you again!

0 Votes 0 ·
Show more comments