简体   繁体   English

按下ENTER键提交表单

[英]Submit FORM on ENTER key pressed

OS: Windows Mobile 5 / Compact .NET Framework (Form.AcceptButton() NOT available) 操作系统:Windows Mobile 5 / Compact .NET Framework(不提供Form.AcceptButton())
I am showing a modal form with ShowDialog(). 我正在用ShowDialog()显示模态形式。 I want to be able to submit the modal form on hitting the ENTER key. 我希望能够在按ENTER键后提交模式表单。 I am able to grab the KEYDOWN EVENT when ENTER key is pressed. 按下ENTER键时,我可以抓住KEYDOWN事件。
Any other solution will be appreciated too. 任何其他解决方案也将不胜感激。

public class Prompt
        {
            public static string AcceptTagPrice()
            {
                Form prompt = new Form();
                prompt.WindowState = System.Windows.Forms.FormWindowState.Maximized;
                prompt.TopMost = true;
                prompt.BackColor = System.Drawing.Color.White;
                prompt.KeyPreview = true;
                prompt.MaximizeBox = true;
                Label textLabel = new Label() { Text = "Enter Price", Left = 20, Top = 50, Width = 200, TextAlign = ContentAlignment.TopCenter, ForeColor = System.Drawing.Color.Green, Font = new System.Drawing.Font("Tahoma", 11F, System.Drawing.FontStyle.Bold) };
                TextBox textBox = new TextBox() { Left = 20, Top = 100, Size = new System.Drawing.Size(202, 31), BackColor = System.Drawing.Color.LightGray }; ;
                Button confirmation = new Button() { Text = "Submit", Left = 30, Top = 140, Size = new System.Drawing.Size(121, 33), BackColor = System.Drawing.Color.LightSkyBlue };
                confirmation.Click += (sender, e) => { bool k = IsValidPrice(textBox.Text); if (k) { prompt.Close(); } else { textBox.Focus(); } };
                prompt.KeyDown += new System.Windows.Forms.KeyEventHandler(Prompt.ModalForm_KeyDown);
                prompt.Controls.Add(confirmation);
                prompt.Controls.Add(textLabel);
                prompt.Controls.Add(textBox);
                textBox.Focus();
                prompt.Activate();
                prompt.ShowDialog();
                return textBox.Text.ToString().Trim();
            }

            public static bool IsValidPrice(string price)
            {
                if (!Regex.IsMatch(price, @"^[0-9]\d*(\.\d+)?$"))
                {
                    MessageBox.Show("Please enter a valid price", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                    return false;
                }
                else
                {
                    return true;
                }
            }

            private static void ModalForm_KeyDown(object sender, KeyEventArgs e)
            {
                if (e.KeyData == Keys.Enter)
                {
                      Messagebox.Show("Enter Key Pressed");
                    // BUT DUNNO HOW TO TRIGGER CONFIRMATION BUTTON CLICK EVENT 
                }
            }
        }

Sounds like you just need to call confirmation.PerformClick() when the Enter key is pressed. 听起来您只需confirmation.PerformClick() Enter键即可调用confirmation.PerformClick() The easiest way to do this is to pass along the confirmation button as a part of the key press. 最简单的方法是将confirmation按钮作为按键的一部分传递。

prompt.KeyDown += (sender, e) => ModalForm_KeyDown(sender, e, confirmation);

...

private static void ModalForm_KeyDown(object sender, KeyEventArgs e, Button confirmation) {
  if (e.KeyData == Keys.Enter) {
    Messagebox.Show("Enter Key Pressed");
    confirmation.PerformClick();
  }
}

** EDIT ** **编辑**

Apparently PerformClick isn't available on compact framework. 显然, PerformClick在紧凑型框架上不可用。 In that case I would just add a public method which is responsible for closing the Form . 在那种情况下,我只需要添加一个public方法来关闭Form The button click handler would just feed into this method so that there is a single code path 按钮单击处理程序将仅输入此方法,因此只有一个代码路径

I would recommend instead separating the button click into a method which you can then call 我建议改为将按钮单击分为一种方法,然后可以调用该方法

Edited to account for Compacted Framework. 编辑以考虑压缩框架。

Add a name property to your textBox 将名称属性添加到您的文本框

TextBox textBox = new TextBox() { Name = "the_textBox" ....

change your confirmation.Click to this 更改您的确认。单击此处

confirmation.Click += (sender, e) => { Confirmation_Click(prompt); };

add this method 添加此方法

private static void Confirmation_Click(Form prompt)
{
    TextBox textBox = prompt.Controls.Find("the_textBox", false).FirstOrDefault() as TextBox;
    if(textBox == null)
        return; //uhm weird

    bool k = IsValidPrice(textBox.Text);
    if (k)
        prompt.Close();
    else 
        textBox.Focus();
}

Replace your KeyDown Method with this 以此替换您的KeyDown方法

private static void ModalForm_KeyDown(object sender, KeyEventArgs e)
{
    Form form = sender as Form;
    if(form == null)
        return; //uhm weird

    if (e.KeyData == Keys.Enter)
    {
        Confirmation_Click(form);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM