获取“system.windows.forms.keypresseventargs”不包含'keycode'的定义“

时间:2013-10-06 03:46:00

标签: c#

我试图在我的应用程序中按一个按键,但是当我尝试编译时,我看到以下错误:“System.Windows.Forms.KeyPressEventArgs'不包含'KeyCode'的定义” 我想要做的是:当用户按下10键上的“1”键时,我希望它按下我的应用程序中的“1”按钮。我正在使用的代码如下: 注意:我正在使用VS 2010。 using指令在这里:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

代码:

private void frmMain_KeyPress(object sender, KeyPressEventArgs e)
    {
        switch  (e.KeyCode)
        {
            case Keys.NumPad0:
                e.Handled = true;
                btnZero.PerformClick();
                break;
            case Keys.NumPad1:
                e.Handled = true;
                btnOne.PerformClick();
                break;
            case Keys.NumPad2:
                e.Handled = true;
                btnTwo.PerformClick();
                break;
            case Keys.NumPad3:
                e.Handled = true;
                btnThree.PerformClick();
                break;
            case Keys.NumPad4:
                e.Handled = true;
                btnFour.PerformClick();
                break;
            case Keys.NumPad5:
                e.Handled = true;
                btnFive.PerformClick();
                break;
            case Keys.NumPad6:
                e.Handled = true;
                btnSix.PerformClick();
                break;
            case Keys.NumPad7:
                e.Handled = true;
                btnSeven.PerformClick();
                break;
            case Keys.NumPad8:
                e.Handled = true;
                btnEight.PerformClick();
                break;
            case Keys.NumPad9:
                e.Handled = true;
                btnNine.PerformClick();
                break;
            case Keys.Add:
                e.Handled = true;
                btnPlus.PerformClick();
                break;
            case Keys.Subtract:
                e.Handled = true;
                btnMinus.PerformClick();
                break;
            case Keys.Divide:
                e.Handled = true;
                btnDivide.PerformClick();
                break;
            case Keys.Decimal:
                e.Handled = true;
                btnDecimal.PerformClick();
                break;
            case Keys.Enter:
                e.Handled = true;
                btnEquals.PerformClick();
                break;
        }
    }

1 个答案:

答案 0 :(得分:1)

我发现如果我从使用

切换
frmMain_KeyPress(object sender, KeyPressEventArgs e)

frmMain_KeyDown(object sender, KeyEventArgs e) 

我可以让它发挥作用。