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.
Read the documentation. You simply add the code to your form.
AHH thank you! After doing some debugging to see what ProcessCmdKey does I was finally able to get it to work! Thank you again!
Your very welcome.
Sign in to comment