简体   繁体   English

Windows窗体计算器-输入的数字键

[英]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.

相关问题 有没有办法在 c# Windows 桌面应用程序中按住左 ALT 键的同时发送 Numpad3 + Numpad4 键以获得双引号? - Is there a way to Send Numpad3 + Numpad4 keys while holding Left ALT in c# Windows Desktop Applicationto get double quotes? 如何在Windows窗体计算器中的计算器中创建零按钮 - how to create a zero button in calculator in windows form calculator Windows 窗体计算器中的错误仅适用于 2 个值 - Error in windows form calculator for 2 values only C# Windows 窗体贷款计算器 - C# Windows Form Loan Calculator 如何通过 Winform 中的数字键控制按钮? - How to Control Buttons via Numpad Keys in Winform? C#-我想从数字键盘上的两个键(从后台)获取用户输入,并使这些输入增加或减少一个int计数器 - C# - I want to get user input (from the background) from two keys on the numpad and make those inputs raise or lower an int counter 将密钥关联到Windows窗体上的按钮 - Associating keys to buttons on a Windows Form 按下ALT键时,Keys.Numpad0作为Keys.System进入 - Keys.Numpad0 comes in as Keys.System when ALT is pressed 将小键盘输入转换为他的int值 - Convert numpad key input to his int value 如何在计算器Windows窗体应用程序中正确使用ProcessCmdKey? - How to correctly use ProcessCmdKey in a calculator windows form application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM