我试图在我的应用程序中获取一个按键来按下按钮,但是当我尝试编译时看到以下错误:“System.Windows.Forms.KeyPressEventArgs'不包含'KeyCode'的定义”我想要做什么是:当用户按下 10 键上的“1”键时,我希望它在我的应用程序中按下“1”按钮。我正在使用的代码附在下面:注意:我使用的是 VS 2010。使用指令在这里:
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;
}
}