通过按Enter在TEXBOX中激活按钮

发布于 2025-01-22 01:03:46 字数 273 浏览 5 评论 0原文

我是在编码中的新手,我试图通过按Enter来从文本框中激活我的按钮。我试图用Google获取答案,但Visual Studios表示它不起作用。 我在搜索答案时阅读的最常见的内容是下面的代码。现在问题是Visual Studios向我显示key Code行作为错误。有人可以帮我弄清楚为什么这是错误的吗?

if (e.Keycode == Keys.Enter)
{
   button1_Click(this, new EventArgs());
}

Im new here in Coding and im Trying to activate my button from a Textbox by pressing enter. I tried to google for the Answer but Visual Studios say it doesnt work.
The most common Thing i read while searching for an Answer was the code down below. Now the problem is Visual Studios shows me the Keycodeline as error. Can somebody help me figure out why its an Error?

if (e.Keycode == Keys.Enter)
{
   button1_Click(this, new EventArgs());
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

氛圍 2025-01-29 01:03:46
  1. 如果您没有,则应注册到keydown事件。
    您可能已经在代码中有类似的东西。它是通过使用设计器中的GUI选项或在编程上使用以下操作来完成

  1. 的 如果处理程序,key代码应用资本C拼写,即:key Code

    完整事件处理程序方法的示例:

     私有void textbox1_keydown(对象发送者,keyeventargs e)
     {
         if(e.keycode == keys.enter)
         {
             button1_click(this,new EventArgs());
         }
     }
     
  1. You should register to the KeyDown event, in case you haven't.
    You might already have something like that in your code. It is done by either using the GUI options in the designer, or programatically using something like:

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

  1. In the event handler, Keycode should be spelled with capital C, i.e.: KeyCode.

    An example for a complete event handler method:

     private void textBox1_KeyDown(object sender, KeyEventArgs e)
     {
         if (e.KeyCode == Keys.Enter)
         {
             button1_Click(this, new EventArgs());
         }
     }
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文