点击进入提交表格窗口表格C# [英] HIT ENTER SUBMIT FORM WINDOW FORM C#

查看:105
本文介绍了点击进入提交表格窗口表格C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当按下Enter键盘时,然后提交表单

how do that when hit the enter form key board then submit the form

推荐答案

尝试一下:)
try this :)
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
       {
           if (keyData == Keys.Enter)
           {
              //submit logic
           }

           return base.ProcessCmdKey(ref msg, keyData);
       }


您可以设置
You can set AcceptButton[^] property to the button in your Form what handles the data entered.

As SA has pointed out, AcceptButton property makes sense when used with form1.ShowDialog(). And remember, just setting the AcceptButton/CancelButton is not enough. This just tell which button should be invoked on ENTER/ESC. We have to set the DialogResult in the Button handler.

button1.DialogResult = System.Windows.Forms.DialogResult.OK;
button2.DialogResult = System.Windows.Forms.DialogResult.Cancel;

form1.AcceptButton = button1;
form1.CancelButton = button2;


您可以设置form1.KeyPreview = true;并处理事件form1.KeyDown.
将KeyPreview设置为true,则该窗体将在事件传递给具有焦点的控件之前接收键事件

You can set form1.KeyPreview = true; and handle event form1.KeyDown.
Setting KeyPreview to true, then the form will receive key events before the event is passed to the control that has focus

form1.KeyPreview = true;
form1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.form1_KeyDown);



抑制ENTER按钮的方法.



Method to suppress ENTER button.

private void form1_KeyDown(object sender, KeyEventArgs e)
{
  if (e.KeyData == Keys.Enter)
  {
    e.SuppressKeyPress = true; // Event should not be sent to the control
    SaveData(); // Get data in form and save.
  }
}


这篇关于点击进入提交表格窗口表格C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆