[英]Windows Form Calculator - Numpad Keys For Input
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. 我目前正在为自己的工作学习c#,正在尝试一些入门项目,因此我决定制作一个计算器,我具有一个简单的计算器的所有功能,但是无法使用numpad键工作对于按键事件或按键事件,我想知道是否有人可以帮助我一些细节,我想将所有数字键盘的按键设置为计算器上相应的按键,这是我为按键事件尝试过的代码我也尝试过用小键盘锁打开和关闭。
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 只是快速编辑,我已经尝试遵循MSDN示例并包含以下内容
private void n1_KeyDown(object sender, KeyEventArgs e) { 私人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 仍然没有成功
Refer to MSDN Key Enum page for reference. 请参考“ MSDN 密钥枚举”页面以供参考。
eg Keys.NumPad0
is on keypad, Keys.D0
is the number key. 例如,
Keys.NumPad0
在小键盘上, Keys.D0
是数字键。 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, eg Keys.Add
for your add. 而且,您可能还想映射运算符,例如
Keys.Add
作为添加。
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....) (相信我,你不会是最后一个掉进那个洞的人。)
尝试使用比较而不是分配。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.