我正在尝试在文本框(WinForm)中添加“KeyPress”事件
this.textBox1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(CheckKeys);
这是在'CheckKeys'里面:
private void CheckKeys(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if (e.KeyChar == (char)13)
{
// Enter is pressed - do something
}
}
这里的想法是,一旦文本框成为焦点并按下“Enter”按钮,就会发生一些事情......
但是,我的机器找不到“KeyPress”事件。我的代码有问题吗?
更新:
我还尝试使用 KeyDown 而不是 KeyPress:
private void textBox1_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
if (e.Key == Key.Return)
// Enter is pressed - do something
}
}
虽然还是不行...