覆盖DataGridView Shift + Space

时间:2012-12-26 20:27:46

标签: vb.net datagridview

在DataGridView中,按SHIFT和SPACE将默认选择整行。我找到的唯一解决方案(在vb.net DataGridView - Replace Shortcut Key with typed character引用)是关闭行选择功能。虽然这有效,但它并不理想,因为我仍然希望能够使用行选择器选择整行(例如,删除行),并将SelectionMode属性更改为除{以外的任何值{1}}我失去了这种能力。有没有办法陷阱SHIFT + SPACE组合并用简单的SPACE替换它?当控件的RowHeaderSelect属性设置为MutiSelect并且True属性设置为SelectionMode时,似乎没有任何关键事件识别出击键,所以我可以'使用那些。

ETA:我想可能会关闭RowHeaderSelect并将选择模式更改为MultiSelect,然后为CellSelect事件添加事件处理程序将起作用... nope。

3 个答案:

答案 0 :(得分:1)

我想出如何实现这一点的最好方法是从DataGridView继承并覆盖ProcessCmdKey方法。然后你可以拦截Shift + Space并发送Space。只需将此类添加到项目中,并将所有DataGridViews切换到MyDataGridViews。我的解决方案从这个DataGridView keydown event not working in C# SO问题中获得灵感(这也解释了为什么Zaggler的解决方案不起作用)和这个SendKeys in ProcessCmdKey: change Shift-Space to just a Space Bytes.com帖子。对不起,但它在C#中。

class MyDataGridView : System.Windows.Forms.DataGridView
{
    protected override bool ProcessCmdKey(ref System.Windows.Forms.Message msg, System.Windows.Forms.Keys keyData)
    {
        if (keyData == (System.Windows.Forms.Keys.Space | System.Windows.Forms.Keys.Shift))
        {
            // DataGridView is dumb and will select a row when the user types Shift+Space 
            // if you have the DGV set so that you can click a row header to select a row (for example, to delete the row)
            // this method will intercept Shift+Space and just send on Space so that the DGV properly handles this. 
            // For example, if I type "ME TYPING IN ALL CAPS" it ends up looking like "METYPINGINALLCAPS".
            // Or if I type "Note: I have some OS thing to talk about" it looks like "Note:Ihave some OSthing to talk about"

            byte[] keyStates = new byte[255];
            UnsafeNativeMethods.GetKeyboardState(keyStates);
            byte shiftKeyState = keyStates[16];
            keyStates[16] = 0; // turn off the shift key
            UnsafeNativeMethods.SetKeyboardState(keyStates);

            System.Windows.Forms.SendKeys.SendWait(" ");

            keyStates[16] = shiftKeyState; // turn the shift key back on
            UnsafeNativeMethods.SetKeyboardState(keyStates);
            return true;
        }
        return base.ProcessCmdKey(ref msg, keyData);
    }

    [System.Security.SuppressUnmanagedCodeSecurity]
    internal static class UnsafeNativeMethods
    {

        [System.Runtime.InteropServices.DllImport("user32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
        public static extern int GetKeyboardState(byte[] keystate);


        [System.Runtime.InteropServices.DllImport("user32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
        public static extern int SetKeyboardState(byte[] keystate);
    }
}

答案 1 :(得分:0)

这对我来说很好....

Private Sub DataGridView1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles DataGridView1.KeyDown
    'Lets see what keys we have down shall we?'
    If My.Computer.Keyboard.ShiftKeyDown And e.KeyCode = Keys.Space Then
        DataGridView1.CurrentCell.Selected = False
    End If
End Sub

这是另一种方式..

Private Sub DataGridView1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles DataGridView1.KeyDown
    'Lets see what keys we have down shall we?'
    If My.Computer.Keyboard.ShiftKeyDown And e.KeyCode = Keys.Space Then
        'SendKeys.Send(Keys.Space)
        DataGridView1.CurrentCell.Selected = False
    End If
End Sub

只是试验一下,我希望能为你做点什么?

答案 2 :(得分:0)

这是我的解决方案:

private void dataGridView_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
    dataGridView.SelectionMode = DataGridViewSelectionMode.CellSelect;
}

private void dataGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
    dataGridView.SelectionMode = DataGridViewSelectionMode.RowHeaderSelect;
}