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

本文关键字:包含 keycode system windows forms keypresseventargs 获取 | 更新日期: 2023-09-27 18:34:08

我试图在我的应用程序中获取一个按键来按下按钮,但是当我尝试编译时看到以下错误:"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;
        }
    }

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

我发现如果我从使用

frmMain_KeyPress(object sender, KeyPressEventArgs e)

frmMain_KeyDown(object sender, KeyEventArgs e) 

我可以让它工作。