Visual Studio C#在文本框中捕获输入键
我正在尝试捕获Windows窗体文本框中的Enter键。我从教程中获得了以下代码片段:Visual Studio C#在文本框中捕获输入键,c#,visual-studio,C#,Visual Studio,我正在尝试捕获Windows窗体文本框中的Enter键。我从教程中获得了以下代码片段: private void textBox1_KeyDown(object sender, KeyEventArgs e) { // // Detect the KeyEventArg's key enumerated constant. // if (e.KeyCode == Keys.Enter) { MessageBox.Show("You pres
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
//
// Detect the KeyEventArg's key enumerated constant.
//
if (e.KeyCode == Keys.Enter)
{
MessageBox.Show("You pressed enter! Good job!");
}
else if (e.KeyCode == Keys.Escape)
{
MessageBox.Show("You pressed escape! What's wrong?");
}
}
但是现在我的代码抛出了一个编译/构建错误:
The event 'System.Windows.Forms.Control.Enter' can only appear on the left hand
side of += or -= Line 44 Column 84
一方面,我不理解错误信息。另一方面,第44行是仅具有换行符的空行
任何建议都将不胜感激
注意。检查设计器文件(form.Designer.cs)
您的设计师应该是:
this.textBox1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.textBox1_KeyDown);
您可能已经订阅了Enter事件。这实际上不是回车键。这是为了在上面,它与休假事件配对。问题在.designer.cs文件中。尝试取消订阅并重新订阅活动。此处显示的代码看起来正常。您如何订阅您的活动以及第43、44和45行上的内容?更好的是:这里是第43、44、45行:`}private void button1_Click(object sender,EventArgs e)`第43行的花括号是上述代码的右括号。我不确定是否订阅了此活动,我只是从教程中复制了代码。这不太好。第43行是花括号。第44行为空。第45行是方法原型。忽略我之前的评论。我查错了文件-(