如何获得关键的pressed,从DataGridView数据单元格KEYDOWN事件 [英] How to get keypressed, keydown events from DataGridView data cells

查看:223
本文介绍了如何获得关键的pressed,从DataGridView数据单元格KEYDOWN事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要接受在单元格编辑键preSS事件的DataGridView 控制。

I need to receive the key press events during cell editing in DataGridView control.

这是我发现在网络上的DataGridView 设计为从的DataGridView通过所有重要事件的单元格编辑的控制,你不能轻易得到这些事件。

From what I have found on the net the DataGridView is designed to pass all key events from DataGridView to the cell editing control and you cannot get these events easily.

我发现这个件code 那捕获这些事件的的DataGrid 的控制,但这并不对的DataGridView

I found this piece of code that traps those events for DataGrid control, but that does not work for DataGridView.

推荐答案

终于想通了。 有这个难题的两个部分 - 从获取单元格编辑控制键和获得密钥从DataGridView的本身。这是我的code。要使用它,你只需要订阅自定义事件:键pressHook

Finally figured out. There are two parts of this puzzle - getting keys from cell editing control and getting keys from the DataGridView itself. Here's my code. To use it, you just need to subscribe to the custom event: keyPressHook.

class KeyPressAwareDataGridView : DataGridView
{

    protected override void OnControlAdded(ControlEventArgs e)
    {
        this.subscribeEvents(e.Control);
        base.OnControlAdded(e);
    }

    protected override void OnControlRemoved(ControlEventArgs e)
    {
        this.unsubscribeEvents(e.Control);
        base.OnControlRemoved(e);
    }

    protected override bool ProcessDataGridViewKey(KeyEventArgs e)
    {
        bool procesedInternally = false;

        if (this.keyPressHook != null)
        {
            this.keyPressHook(this, e);
            procesedInternally = e.SuppressKeyPress;
        }

        if (procesedInternally)
        {
            return true;
        }
        else
        {
            return base.ProcessDataGridViewKey(e);
        }
    }


    private void subscribeEvents(Control control)
    {
        control.KeyDown += new KeyEventHandler(this.control_KeyDown);
        control.ControlAdded += new ControlEventHandler(this.control_ControlAdded);
        control.ControlRemoved += new ControlEventHandler(this.control_ControlRemoved);

        foreach (Control innerControl in control.Controls)
        {
            this.subscribeEvents(innerControl);
        }
    }

    private void unsubscribeEvents(Control control)
    {
        control.KeyDown -= new KeyEventHandler(this.control_KeyDown);
        control.ControlAdded -= new ControlEventHandler(this.control_ControlAdded);
        control.ControlRemoved -= new ControlEventHandler(this.control_ControlRemoved);

        foreach (Control innerControl in control.Controls)
        {
            this.unsubscribeEvents(innerControl);
        }
    }

    private void control_ControlAdded(object sender, ControlEventArgs e)
    {
        this.subscribeEvents(e.Control);
    }

    private void control_ControlRemoved(object sender, ControlEventArgs e)
    {
        this.unsubscribeEvents(e.Control);
    }

    private void control_KeyDown(object sender, KeyEventArgs e)
    {
        if (this.keyPressHook != null)
        {
            this.keyPressHook(this, e);
        }
    }

    public event KeyEventHandler keyPressHook;

}

这篇关于如何获得关键的pressed,从DataGridView数据单元格KEYDOWN事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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