C#:如何取消先前获得焦点的文本框的焦点?

C#: How to cancel the focus of a previousely focused text box?

我有一个应该只包含数字的文本框。检查是在 Leave 事件中进行的。如果文本框包含字符而不是数字,它会提示用户检查他们的输入并在保持专注于文本框的同时重试。

问题是,如果用户按下取消键,文本框仍然保持焦点并且无法单击表单中的其他地方。如果他删除文本框的内容,也会发生同样的情况。我究竟做错了什么?将不胜感激一些帮助!提前致谢!

private void whateverTextBox_Leave(object sender, EventArgs e)
    {
        //checks to see if the text box is blank or not. if not blank the if happens
        if (whateverTextbox.Text != String.Empty)
        {
            double parsedValue;

            //checks to see if the value inside the checkbox is a number or not, if not a number the if happens
            if (!double.TryParse(whateverTextbox.Text, out parsedValue))
            {
                DialogResult reply = MessageBox.Show("Numbers only!" + "\n" + "Press ok to try again or Cancel to abort the operation", "Warning!", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation);

                //if the user presses ok, textbox gets erased, gets to try again
                if (reply == DialogResult.OK)
                {
                    whateverTextbox.Clear();
                    whateverTextbox.Focus();
                }

                //if the user presses cancel, the input operation will be aborted
                else if (reply == DialogResult.Cancel)
                {
                    whateverTextbox.Clear();

                    //whateverTextbox.Text = String.Empty;

                    //nextTextBox.Focus();
                }
            }
        }
    }

为什么不做这样的事情:

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    if (!char.IsDigit(e.KeyChar) && e.KeyChar != (char)Keys.Back)
    {
        e.Handled = true;
        MessageBox.Show("Numbers only!" + "\n" + "Press ok to try again or Cancel to abort the operation", "Warning!");
    }
}