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
if
line in your code. Then try pressing a number-pad digit key and use the watch window to find out whate.KeyChar
is equal to in that case. You can take a look at the other properties ofe
as well. – 15ee8f99-57ff-4f92-890c-b56153