如何prevent用户控件的形式在C#处理键盘输入(箭头键) [英] How to prevent usercontrol form handling keyboard input (arrow keys) in c#

查看:470
本文介绍了如何prevent用户控件的形式在C#处理键盘输入(箭头键)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的用户包含可以选择其他控件,我想实现导航了子控件与方向键的方式

my usercontrol contains other controls that can be selected, i would like to implement way of navigating over child controls with arrow keys

问题是,父母控制研究拦截箭头键,并用它来滚动视图什么是我想要避免的事情。我想解决在导航控制内容由我自己。

The problem is that parent controll intercepts arrow keys and use it to scroll its view what is the thing i want to avoid. I want to solve navigating over contents of control by myself.

我怎么能采取控制标准的行为是由方向键?

How i can take control over standard behaviour that is caused by arrow keys?

在此先感谢 MTH

推荐答案

这通常是通过重写IsInputKey()方法来告诉你想要这些键来完成。然而,这不会对用户控件工作,它从来没有得到的焦点,使的onkeydown方法永远不会运行。相反,覆盖给ProcessCmdKey()方法,在窗体看到击键之前调用。像这样的:

This is normally done by overriding IsInputKey() method to tell that you want these keys. That however won't work for a UserControl, it never gets the focus so the OnKeyDown method never runs. Instead, override the ProcessCmdKey() method, it is called before the form sees the keystroke. Like this:

    protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
        switch (keyData) {
            case Keys.Left: MessageBox.Show("Left!"); break;
            case Keys.Right: MessageBox.Show("Right!"); break;
            default: return base.ProcessCmdKey(ref msg, keyData);
        }
        return true;  // used
    }

这篇关于如何prevent用户控件的形式在C#处理键盘输入(箭头键)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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