按下组合键时聚焦用户控件的子控件

游戏中的玩家

我有一个包含面板的表单,我在其中放置了一个用户控件。
我希望当Ctrl+F按下键组合,我的 UserControl 的子级 TextBox 控件获得焦点。

结构是这样的:

在此处输入图片说明

到目前为止,我已经尝试处理KeyPreviewKeyDown事件。我可以显示一个 MessegeBox:

在此处输入图片说明

但我无法将 TextBox 聚焦在我的 UserControl 中。
我怎么解决这个问题?

吉米

You can probably implement IMessageFilter and handle the Keys combinations you want.
You then have to use Application.AddMessageFilter() to add the message filter and Application.RemoveMessageFilter() to remove it when not needed anymore.

Here, the UserControl's DesignMode property is checked, so the filter is added at run.time only.

Possibly, add a public Property that can add / remove / change combinations of Keys, in case there's a conflict with other controls.

The GetAncestor() function is used to determine whether the Form where the Keys combination is triggered is the Parent Form of this instance of the UserControl.
PreFilterMessage() is called when messages are generated in any Form of the application.
If you instead want to perform an action in any case, even when the combination is generated in another open Form (and, maybe, pop the Parent Form in front), just remove that check.


Filter Control + F.
If you need more filters, as mentioned, use a collection to handle these combinations.

接收到WM_KEYDOWN 时WParam包含虚拟键码。Virtual Key 值等效于Keys枚举器。
ModifierKeys属性包含相关按键当前活动(在Control单独密钥在这里测试,当然你也可以添加其他的快捷键使用,如CTRL+SHIFT)。

using System.ComponentModel;
using System.Runtime.InteropServices;

public partial class SomeUserControl : UserControl, IMessageFilter
{
    public SomeUserControl() => InitializeComponent();
    public bool PreFilterMessage(ref Message m)
    {
        if (m.Msg == WM_KEYDOWN) {
            if (GetAncestor(m.HWnd, 2).Equals(ParentForm.Handle)) {
                if (m.WParam.ToInt32() == (int)Keys.F && ModifierKeys == Keys.Control) {
                    someChildTextBox.Focus();
                }
            }
        }
        return false;
    }

    protected override void OnHandleCreated(EventArgs e) {
        base.OnHandleCreated(e);
        if (!DesignMode) Application.AddMessageFilter(this);
    }

    protected override void OnHandleDestroyed(EventArgs e) {
        if (!DesignMode) Application.RemoveMessageFilter(this);
        base.OnHandleDestroyed(e);
    }

    private const int WM_KEYDOWN = 0x0100;

    [DllImport("user32.dll", SetLastError = true)]
    private static extern IntPtr GetAncestor(IntPtr hWnd, uint flags);
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

按下组合键时执行功能

HostListener分析组合键按下

AHK if 语句 - 基于按下的组合键运行指令

组合键输入和特殊按键按下

当某个单元格的值=“ 1”时,自动按下组合键(例如Ctrl + Alt + x)

当用户按下其他控件/空白时,如何使UITextField失去焦点?

如何使两个键 Ctrl + S 按下组合键?

在Python中按组合键分组

禁用整个用户控件的键盘聚焦

用户控件背景隐藏子控件

在不使用鼠标的情况下检查和取消选中控件,因为当我们按Enter键时,按钮控件被触发

在C#中按名称获取用户控件的子控件

有什么方法可以将焦点设置在Tab键按下时的特定html控件上吗?

按Enter时将Focus设置为DataGridTemplateColumn子控件

控件失去子控件时的捕获事件

Javascript-当用户从移动通知控件中按下播放/暂停时,如何覆盖默认操作?

组合键,比较

根据是否按下组合键,启动第一个系统或第二个系统

如何检查JavaScript中是否按了组合键

使用applescipt按alt + s组合键

如何在子用户控件的组合框中触发双击事件?

WPF将样式应用于用户控件的子控件

如何重新访问之前在子控件中声明的用户控件?

按组合键的一部分选择全部与组合键的其余部分匹配的行?

控件通过后允许按下按钮

键映射:组合键到组合键

仅当使用单独的用户控件时才验证控件

在输入控件之外按Enter键时如何触发按钮单击

每次用户按下javascript键时如何触发事件