简体   繁体   English

在自定义控件中禁用滚动条的焦点

[英]Disable focus for scrollbars in custom control

I've written a custom control in Windows Forms. 我已经在Windows窗体中编写了一个自定义控件。 I dynamically create a vertical scrollbar and capture changes of its value. 我动态创建一个垂直滚动条并捕获其值的更改。

Currently, the scrollbar is the only control, which can acquire focus, so my control acts as panel: it transfers focus to the scrollbar. 当前,滚动条是唯一可以获取焦点的控件,因此我的控件充当面板:它将焦点转移到滚动条。 When user uses the scrollbar, all further keyboard events are transfered to it, instead of control itself. 当用户使用滚动条时,所有其他键盘事件都将传递给它,而不是控件本身。

I wish to disable focus for scrollbar completely and implement appropriate behaviors in the control itself (eg. Page up, page down, home, end etc.). 我希望完全禁用滚动条的焦点,并在控件本身中实现适当的行为(例如,Page Up,Page Down,Home,End等)。 I found a property named "CanFocus", but unfortunatelly it's read-only. 我找到了一个名为“ CanFocus”的属性,但不幸的是它是只读的。

How can I disable the focusing ability for the scrollbar? 如何禁用滚动条的聚焦功能? I know, that I can implement the Focus event and manually transfer the focus to the control, but maybe there is a ready solution in the WF libraries? 我知道,我可以实现Focus事件,并将焦点手动转移到控件,但是WF库中是否有现成的解决方案?

Thanks to Łukasz O. for help. 感谢ŁukaszO.的帮助。

The solution: 解决方案:

public class MyVScrollBar : VScrollBar
{
    public MyVScrollBar()
    {
        SetStyle(ControlStyles.Selectable, false);
    }
}

public partial class UserControl1 : UserControl
{
    protected override void OnKeyDown(KeyEventArgs e)
    {
        MessageBox.Show("KeyDown: " + e.KeyCode.ToString());
        base.OnKeyDown(e);
    }

    protected override bool IsInputKey(Keys keyData)
    {
        if (keyData == Keys.Up || keyData == Keys.Down || keyData == Keys.Left || keyData == Keys.Right)
            return true;
        else
            return base.IsInputKey(keyData);
    }

    public UserControl1()
    {
        SetStyle(ControlStyles.Selectable, true);

        InitializeComponent();
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM