2014-03-03 64 views
2

操作系统:Windows Mobile 5/Compact .NET Framework(Form.AcceptButton()不可用)
我使用ShowDialog()显示模态窗体。我希望能够按下ENTER键提交模态表单。按下ENTER键后,我可以抓取KEYDOWN EVENT。
任何其他解决方案也将受到赞赏。
按ENTER键提交FORM

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

设置按钮的DialogResult属性。 –

回答

0

我会建议,而不是分离的按钮,点击进入的方法,你可以调用

编辑以占压实框架。

name属性您confirmation.Click添加到您的textBox

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); 
    } 
} 
+0

感谢您的回复。不幸的是,PerformClick()属性在紧凑框架中不可用。 – LPP

+0

@LPP我已经做了改变,避免使用PerformClick(),现在应该在紧凑框架上工作 – BenVlodgi

+0

工作!精简框架dint支持prompt.controls.Find属性(无赖),但做了一些更改后,您的解决方案确实有效(我在Confirmation_Click方法和Click事件中传递了文本框)非常感谢! – LPP

0

听起来像是你只需要按下回车键时要调用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。按一下按钮处理程序将只给这个方法,以便有一个单一的代码路径

+0

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