WinPress UserControl上不会触发KeyPress事件 [英] KeyPress event doesn't trigger on WinForm UserControl

查看:338
本文介绍了WinPress UserControl上不会触发KeyPress事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的winforms应用程序中有一个用户控件,其中包含KeyPress事件中的以下代码:

I have a user control in my winforms application with this code in the KeyPress event:

private void txtCodigo_KeyPress(object sender, KeyPressEventArgs e)
{
    if ((this.txtCodigo.Text.Length == 0) && (e.KeyChar == '\r'))
    {
        this.Limpiar();
        if (LimpiarEvento != null)
            LimpiarEvento(this, new EventArgs());
        if (NextControl != null)
            NextControl(this, new EventArgs());
    }

    if ((this.txtCodigo.Text.Length > 0) && (e.KeyChar == '\r'))
        this.txtCodigo_Leave(sender, e);

    if (NUMEROS.IndexOf(e.KeyChar) < 0)
    {
        e.Handled = true;
    }
}

现在,我的问题是,该UserControl在我的应用程序中处于多种形式,并且当我在txtCodigo文本框中按Enter键时,它可以完美运行,但是在一种形式中,不会触发Enter键.即使我设置了断点,它也不会触发. 为什么会发生这种情况?

Now, my problem is that this UserControl is in many forms in my application and works perfectly when i press the enter key in the txtCodigo textbox but in one form the enter key is not being fired. Even if i put a breakpoint it doesn't fire at all. Why this could be happening?

我只是尝试向表单本身添加一个KeyPress事件,并将KeyPreview设置为true,并且没有捕获Enter键...除了Enter键之外,其他所有键均被捕获.我真的不知道发生了什么.是的...回车键在键盘上可以正常使用:)

I just try to add a KeyPress event to the form itself and set the KeyPreview to true and the Enter key is not being captured... all the other keys are being captured except the Enter key. I really don't know what is happening. And yes... the enter key works fine in the keyboard :)

推荐答案

该特定表单是否设置了AcceptButton?

Does that particular form have its AcceptButton set?

(已在评论中回答)

此SO问题可能与修复表单有关:防止WinForm AcceptButton处理返回键

This SO question might be relevant for fixing up the Form: Prevent WinForm AcceptButton handling Return key

这篇关于WinPress UserControl上不会触发KeyPress事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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