禁用调度程序控件上的箭头键

时间:2016-10-13 10:49:32

标签: c# winforms devexpress

有没有办法禁用左右箭头键?因为我不希望用户看到10 october之前的日期和16 october之后的日期

enter image description here

我试过这段代码

       private void schedulerControl1_KeyDown(object sender, KeyEventArgs e)
    {
        if(e.KeyCode == Keys.Right)
        {

            e.Handled = true;
        }
    }

它仍然可以点击箭头键,我检查schedulercontrol属性没有keypreview。或者可能设置禁用用户无法点击或关注单元格

n.b:iam使用devexpress与组件scheduler

2 个答案:

答案 0 :(得分:1)

您可以使用IKeyboardHandlerService。

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        IKeyboardHandlerService oldKeyboardHandler = (IKeyboardHandlerService)schedulerControl1.GetService(typeof(IKeyboardHandlerService));
        if (oldKeyboardHandler != null)
        {
            MyKeyboardHandlerService newKeyboardHandler = new MyKeyboardHandlerService(schedulerControl1, oldKeyboardHandler);
            schedulerControl1.RemoveService(typeof(IKeyboardHandlerService));
            schedulerControl1.AddService(typeof(IKeyboardHandlerService), newKeyboardHandler);
        }
    }
}

public class MyKeyboardHandlerService : KeyboardHandlerServiceWrapper
{
    IServiceProvider provider;

    public MyKeyboardHandlerService(IServiceProvider provider, IKeyboardHandlerService service)
        : base(service)
    {
        this.provider = provider;
    }

    public override void OnKeyDown(KeyEventArgs e)
    {
        if (!(e.KeyCode == Keys.Left|| e.KeyCode == Keys.Right))
                        base.OnKeyDown(e);
    }
}

答案 1 :(得分:1)