Winform的DataGridView的手柄Tab键和方向键 [英] Winform Datagridview handle tab and arrow keys

查看:810
本文介绍了Winform的DataGridView的手柄Tab键和方向键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要处理的DataGridView的单元格中的KeyDown事件。我用下面的代码来获取对细胞KeyDown事件:

I want to handle the KeyDown event on the DataGridView cell. I use the following code to get the KeyDown event on cell:

private void dgvData_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {

            var tb = (DataGridViewTextBoxEditingControl)e.Control;

            tb.KeyDown += cell_KeyDown;
        }



不过貌似我不能处理像标签和箭头的一些特殊键。这些密钥不走我的cell_KeyDown方法。所以我尽量处理他们在DataGridView的KeyDown事件:

But looks like I cannot handle some special keys like tab and arrows. Those keys does not go to my cell_KeyDown method. So I try to handle them in the DataGridView's KeyDown event:

private void dgvData_KeyDown(object sender, KeyEventArgs e)
{
// handle keys
}

在这种情况下,我仍然不能捕捉Tab键。我可以捕获箭头键,然而,处理我的自定义事件之后,它仍然由箭头前进到其它细胞。我想留在细胞

In that event, I still cannot capture the Tab key. I can capture the arrow keys, however, after handling my custom events, it still goes to other cells by the arrow. I want to stay in the cell.

然后我扩展了DataGridView的是这样的:

Then I extend the DataGridView like this:

class DataGridViewSp : DataGridView
    {

        protected override bool ProcessDialogKey(Keys keyData)
        {
            if (keyData == Keys.Tab)
            {
                //todo special handling
                return true;
            }

            else if (keyData == Keys.Down)
            {
                //todo special handling
                return true;
            }

            else if (keyData == Keys.Up)
            {
                //todo special handling
                return true;
            }
            else
            {
                return base.ProcessDialogKey(keyData);
            }
        }
    }

现在我可以捕获标签键在此重写ProcessDialogKey方法。但尽管如此,它不捕获向下和向上箭头键。这有什么错?

Now I can capture the Tab key in this overridden ProcessDialogKey method. But Still, it does not capture the Down and Up arrow keys. Is there anything wrong?

当在单元格编辑模式,它处理Tab和箭头键在我的方式,留在单元格中的完美的解决方案将是。当在网格中,箭头键和Tab键在一个正常的方式工作。

The perfect solution would be when in cell editing mode, it handles tab and arrow keys in my way and stay in the cell. When in the grid, arrow and tab keys work in a normal way.

推荐答案

而不是 ProcessDialogKey <的/ code>使用 ProcessCmdKey 。然后,你将捕获所有你需要的按键。

Instead of ProcessDialogKey use ProcessCmdKey. Then you will capture all the keys you need.

protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
    if (keyData == Keys.Tab)
    {
        //todo special handling
        return true;
    }
    return base.ProcessCmdKey(ref msg, keyData);
}

这篇关于Winform的DataGridView的手柄Tab键和方向键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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