【问题标题】:Submit FORM on ENTER key pressed按下 ENTER 键提交 FORM
【发布时间】:2014-04-04 19:03:41
【问题描述】:

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

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

【问题讨论】:

  • 设置按钮的DialogResult属性。

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


【解决方案1】:

听起来您只需要在按下 Enter 键时调用 confirmation.PerformClick()。最简单的方法是传递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();
  }
}

** 编辑 **

显然PerformClick 在紧凑框架上不可用。在这种情况下,我只需添加一个public 方法,该方法负责关闭Form。按钮单击处理程序只会输入此方法,以便有一个单一的代码路径

【讨论】:

  • 感谢您的回复。不幸的是,PerformClick() 属性在紧凑框架中不可用。我仍在努力寻找解决方案。
【解决方案2】:

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

根据 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);
    }
}

【讨论】:

  • 感谢您的回复。不幸的是,PerformClick() 属性在紧凑框架中不可用。
  • @LPP 我已经做出改变,避免使用 PerformClick(),现在应该可以在紧凑的框架上工作
  • 成功了! Compact Framework 也支持 prompt.Controls.Find 属性(很糟糕),但是在进行了一些更改之后,您的解决方案确实有效(我在 Confirmation_Click 方法和 Click 事件中传递了文本框)非常感谢!
  • 好!...我最初也通过了文本框,但为简单起见将其取出,但我想我应该确保 .Find 是一件事。不过,我很高兴你让它工作了。
猜你喜欢
  • 2011-11-05
  • 1970-01-01
  • 1970-01-01
  • 2011-05-29
  • 2010-10-28
  • 2016-04-23
  • 1970-01-01
  • 1970-01-01
  • 2016-10-22
相关资源
最近更新 更多