MessageBox 不抑制键盘 Enter 键 [英] MessageBox does not suppress keyboard Enter key

查看:26
本文介绍了MessageBox 不抑制键盘 Enter 键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个按钮,在 Click 事件中,我对 Form 中的一些 TextBox 进行了一些验证.如果 TextBox 没有通过验证,那么我会强制使用 Focus(用户必须在该 TextBox 中输入一些字符).如果用户按下 Enter 键,我的 TextBox 类已经有一些代码可以转到下一个控件.

I have a button which, on Click event I made some validations upon some TextBoxes in my Form. If a TextBox does not pass the validation, then I force the Focus to it (user must enter some characters in that TextBox). My TextBox class already have some code to go to the next control if user will press Enter key.

MyTextBox.cs 类

MyTextBox.cs class

public class MyTextBox : TextBox
{
    public MyTextBox(){
        KeyUp += MyTextBox_KeyUp;
        KeyDown += MyTextBox_KeyDown;
    }

    private void MyTextBox_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Enter)
        {
            // This will suppress Blink sound
            e.SuppressKeyPress = true;
        }
    }

    private void MyTextBox_KeyUp(object sender, KeyEventArgs e)
    {
        if ((e.KeyCode == Keys.Enter) || (e.KeyCode == Keys.Return))
        {
            // This will go to the next control if Enter will be pressed.
            SendKeys.Send("{TAB}");
        }
    }
}

表单的按钮点击事件:

private void BtnPrint_Click(object sender, EventArgs e){
    // txtName is based on MyTextBox class
    if(txtName.Text.Length == 0){
        MessageBox.Show("Name field could not be empty! Please fill the Name!", "Error Message",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
        // If I Click the OK button, txtName will stay focused in the next line,
        // but if I press Enter key, it will go to the next control.
        txtName.Focus();
        return;
    }
    // Some other validations ...
    // Calling printing method ...
}

当用户点击 MessageBox 中的 Enter 键时,我如何停止对文本框的关注?

How do I stop the loosing focus on my textboxes when user hit Enter key in that MessageBox?

推荐答案

MessageBox 在某些情况下可能会导致重入问题.这是一个经典的.

A MessageBox can cause re-entrancy problems in some occasions. This is a classic one.

在这种特定情况下,当按下 Enter 键向对话框发送确认时,KeyUp 事件 重新输入消息循环并被分派到活动控件.TextBox,在这里,因为这个调用:txtName.Focus();.

In this specific case, when the Enter key is pressed to send a confirmation to the dialog, the KeyUp event re-enters the message loop and is dispatched to the active control. The TextBox, here, because of this call: txtName.Focus();.

当这种情况发生时,TextBox 的 KeyUp 事件处理程序中的代码再次被触发,导致 SendKeys.Send("{TAB}");.

When this happens, the code in the TextBox's KeyUp event handler is triggered again, causing a SendKeys.Send("{TAB}");.

有不同的方法可以解决这个问题.在这种情况下,只需使用 TextBox.KeyDown 事件来抑制 Enter 键并移动焦点:

There are different ways to solve this. In this case, just use the TextBox.KeyDown event to both suppress the Enter key and move the focus:

private void MyTextBox_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Enter)
    {
        e.SuppressKeyPress = true;
        SendKeys.Send("{TAB}");
    }
}

另外,参见(例如):

在 MessageBox 中按 Enter 会触发控件 KeyUp 事件
MessageBox 导致键处理程序忽略 SurpressKeyPress

作为处理类似情况的不同方法.

as different methods to handle similar situations.

这篇关于MessageBox 不抑制键盘 Enter 键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆