Question

OS: Windows Mobile 5 / Compact .NET Framework (Form.AcceptButton() NOT available)
I am showing a modal form with ShowDialog(). I want to be able to submit the modal form on hitting the ENTER key. I am able to grab the KEYDOWN EVENT when ENTER key is pressed.
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 
                }
            }
        }
Était-ce utile?

La solution

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

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

Autres conseils

Sounds like you just need to call confirmation.PerformClick() when the Enter key is pressed. The easiest way to do this is to pass along the confirmation button as a part of the key press.

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. In that case I would just add a public method which is responsible for closing the Form. The button click handler would just feed into this method so that there is a single code path

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top