UserControl KeyDown 事件不会针对撤销 Windows 应用程序 KeyDown 事件而触发 [英] UserControl KeyDown Event Not Fire Against Revoke Of Windows Application KeyDown Event

查看:36
本文介绍了UserControl KeyDown 事件不会针对撤销 Windows 应用程序 KeyDown 事件而触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 UserControl 并且面临 KeyDown 事件的问题.我的 UserControls 将显示反对撤销 windows 窗体 keydown 事件,如下所示:

I have UserControl and facing problem of KeyDown Event. My UserControls will shows against revoke of windows forms keydown event like below:

用户控件的事件:

private void UserControl_KeyDown(object sender,KeyEventArgs e)
{
        if ((Keys)e.KeyCode == Keys.Escape) 
        {
            this.Visible = false;

        }

}

上述事件将不得不隐藏 UserControl,但由于撤销了 windows 窗体 keydown,该事件不会触发,如下所示:

The above event will have to hide the UserControl but the event not fire due to revoke of windows forms keydown as below:

Windows 窗体:

private void button1_Click(object sender, EventArgs e)
{
        panel1.SendToBack();
        panel2.SendToBack();
        panel3.SendToBack();
        panel4.SendToBack();
        am.Focus();
        this.KeyDown -= new KeyEventHandler(Form1_KeyDown);

}

这将显示 UserControls,因为 UserControls 被添加到 windows 窗体中,如下所示:

This will shows the UserControls as the UserControls are added to windows forms by as below:

    private UserControl.UserControl am = new UserControl.UserControl();
    public Form1()
    {
        InitializeComponent();
        this.Controls.Add(am);

    }

我想针对 UserControl 的可见性撤销 winform 的 keydown 事件并触发 UserControl 的 keydown 事件以隐藏 UserControl 但它做得不好.UserControl 事件的 keydown 事件不会触发.不知道为什么?.如何以正确的方式做同样的事情?.

I want to revoke the keydown event of winform against visible of UserControl and fire the keydown event of UserControl for hide the UserControl but it’s not doing well. The keydown event of UserControl event is not fire. Don’t know why?. How to do the same in proper way?.

推荐答案

键盘通知发布到具有焦点的控件.几乎从来都不是 UserControl,它不需要焦点.即使您使用 Focus() 方法显式设置焦点,它也会立即将焦点传递给其子控件之一.UserControl 被设计为只是其他控件的容器.

Keyboard notifications are posted to the control with the focus. Which is almost never a UserControl, it doesn't want the focus. Even if you explicitly set the focus with the Focus() method, it will immediately pass it off to one of its child controls. UserControl was designed to be just a container for other controls.

改为覆盖 ProcessCmdKey() 方法.将此代码粘贴到控件类中:

Override the ProcessCmdKey() method instead. Paste this code into the control class:

    protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
        if (keyData == Keys.Escape) {
            this.Visible = false;
            return true;
        }
        return base.ProcessCmdKey(ref msg, keyData);
    }

请注意,使用 Escape 键并不是最好的主意,您放置用户控件的表单很可能需要该键.就像一个对话框.

Beware that using the Escape key is not the greatest idea, the Form you put your user control on may well have a need for that key. Like a dialog.

这篇关于UserControl KeyDown 事件不会针对撤销 Windows 应用程序 KeyDown 事件而触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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