Visual Studio C# Capturing Enter Key In Textbox

本文关键字:Key In Textbox Enter Capturing Studio Visual | 更新日期: 2023-09-27 18:35:59

我正在尝试捕获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 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 行是一个只有换行符的空行。

任何建议不胜感激。

问候。

Visual Studio C# Capturing Enter Key In Textbox

检查设计器文件(窗体。设计师.cs)

您的设计师应该是:

this.textBox1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.textBox1_KeyDown);

您可能已订阅 Enter 事件。这实际上不是 Enter 键。那是为了在上面,它与离开事件配对。