【问题标题】:How to type text in TextBox and then be able to press return to click on Button? [duplicate]如何在 TextBox 中输入文本,然后按回车键单击按钮? [复制]
【发布时间】:2012-02-09 11:14:57
【问题描述】:

我有一个文本框和一个按钮。在 TextBox 中,我想输入文本,然后能够单击 Enter 并按下我的按钮。当我按下按钮时,我可以使用 button1.Focus();

重定向到文本框

现在,如果我输入文本并按 Enter,则不会发生任何事情。我可以使用 tab 切换到一个按钮,但我只是想知道我是否可以直接按 enter。

有什么想法吗?

【问题讨论】:

    标签: c# winforms button textbox


    【解决方案1】:

    在表单的属性中,将按钮设置为AcceptButton 属性的值。

    【讨论】:

      【解决方案2】:

      一个简单的解决方案是:

      private void button1_Click(object sender, EventArgs e)
      {
          MessageBox.Show("Click!");
      }
      
      private void textBox1_KeyUp(object sender, KeyEventArgs e)
      {
          if (e.KeyCode == Keys.Enter)
              button1_Click(textBox1, new EventArgs());
      }
      

      如果您在多个地方使用它,我建议您创建一个包含TextBoxButton 的用户控件,以封装此行为。

      【讨论】:

        【解决方案3】:

        您可以在文本框中添加 KeyPressed 事件。在此事件中检查按下的键是否为回车,如果是,则调用按钮的单击事件。

        【讨论】:

          【解决方案4】:

          我认为这种方式对于您的问题来说是一个非常聪明的例程。此过程还允许您检查 TAB 按下。顺便说一句,直接从:

          http://msdn.microsoft.com/en-us/library/system.windows.forms.control.isinputkey.aspx

          您只需将 microsoft 示例中的 Keys.Tab 更改为 Key.Enter 即可。请记住,当此文本框与 AutoComplete 例程一起使用时,在构造函数之外或在构造函数重载中实例化它可能会很方便。这是文本框的实例化的样子: ReturnTextBox returntextbox = new ReturnTextBox();

          using System.Windows.Forms;
          
          public class Form1 : Form
          {
              public Form1()
              {
                  FlowLayoutPanel panel = new FlowLayoutPanel();
          
                  ReturnTextBox returntextbox = new ReturnTextBox();
                  returntextbox.Text = "returntextbox";
                  panel.Controls.Add(returntextbox);
          
                  TextBox textBox1 = new TextBox();
                  textBox1.Text = "Normal TextBox";
                  panel.Controls.Add(textBox1);
          
                  this.Controls.Add(panel);
              }
          }
          
          class ReturnTextBox : TextBox
          {
              protected override bool IsInputKey(Keys keyData)
              {
                  if (keyData == Keys.Enter)
                  {
                      return true;
                  }
                  else
                  {
                      return base.IsInputKey(keyData);
                  }
              }
          
              protected override void OnKeyDown(KeyEventArgs e)
              {
                  if (e.KeyData == Keys.Enter)
                  {
                      this.SelectedText = "    ";                
                  }
                  else
                  {
                      base.OnKeyDown(e);
                  }
              }
          }
          

          【讨论】:

            猜你喜欢
            • 2022-09-27
            • 1970-01-01
            • 1970-01-01
            • 2021-05-14
            • 2015-02-17
            • 2017-05-22
            • 2013-06-20
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多