c# - 按下 ENTER 键后提交表格

标签 c# keydown showdialog submit-button windows-mobile-5.0

操作系统:Windows Mobile 5/Compact .NET Framework(Form.AcceptButton() 不可用)
我正在使用 ShowDialog() 显示模态窗体。我希望能够在按下 ENTER 键时提交模态表单。当按下 ENTER 键时,我能够获取 KEYDOWN EVENT。
任何其他解决方案也将不胜感激。

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 
                }
            }
        }

最佳答案

我建议改为将按钮单击分离到一个方法中,然后您可以调用该方法

经过编辑以说明 Compacted Framework。

给你的文本框添加一个名称属性

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

更改您的确认。点击这里

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

添加这个方法

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();
}

用这个替换你的 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);
    }
}

关于c# - 按下 ENTER 键后提交表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22151728/

相关文章:

c# - List.AddRange 调用 List.Add 吗?

jquery - 仅当输入未获得焦点时才按下键

javascript - 跟踪是否在输入字段中按下了退格键或删除键

flutter - 在 flutter 中使用 showDialog 会引发错误 - "' !_debugLocked' : is not true. "

c# - 将 Newtonsoft JSON 序列化为字节数组

c# - 将基数 2 转换为基数 10

c# - 使用linq检查用户名是否已存在于数据库中

c# - WPF 如何管理 ENTER 命中 TextBox 以进行消息传递

c# - 如何等到对话框真正关闭

c# - 比 DialogResult 包含更多信息的自定义 ShowDialog