Reputation: 29
I am currently in the middle of learning c# for my job, I have been trying some starter projects and i decided to make a calculator, I have all of the functions of a simple calculator working, however i can not get the numpad keys to work with a keypress event or a keydown event, I am wondering if someone can help me in some detail, i am wanting to sett all of the numpad keys to the corrspondings ones on the calculator, here is the code i have tried for the keypress event and i have also tried this with numpad lock on and off.
private void n1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar = '1')
{
e.Handled = true;
n1.PerformClick();
}
}
Just a quick edit, i have tried to follow MSDN example and include the following
private void n1_KeyDown(object sender, KeyEventArgs e) {
if (e.KeyCode < Keys.D0 || e.KeyCode > Keys.D9)
{
if (e.KeyCode < Keys.NumPad0 || e.KeyCode > Keys.NumPad9)
{
nonNumberEntered = true;
}
}
And still no success
Upvotes: 0
Views: 1820
Reputation: 2618
Check what the equals operator should be.
if (e.KeyChar == '1')
(You won't be the last person to fall down that particular hole, trust me....)
Upvotes: 0
Reputation: 1514
Refer to MSDN Key Enum page for reference.
e.g. Keys.NumPad0
is on keypad, Keys.D0
is the number key. So you want to do something like this
if (e.KeyCode == Keys.NumPad0 || e.KeyCode == Keys.D0)
And also you probably want to map the operators, e.g. Keys.Add
for your add.
Upvotes: 1