Vb.net 覆盖DataGridView Shift+空格

Vb.net 覆盖DataGridView Shift+空格,vb.net,datagridview,Vb.net,Datagridview,在DataGridView中,按SHIFT键和空格键将默认选择整行。我在中找到的唯一解决方案是关闭行选择功能。虽然这样做有效,但并不理想,因为我仍然希望能够使用行选择器选择整行,例如,删除行,并且通过将SelectionMode属性更改为RowHeaderSelect以外的任何属性,我将失去这种能力。有没有一种方法可以将SHIFT+空格组合设为陷阱,并将其替换为简单空格?当控件的multiselect属性设置为True,SelectionMode属性设置为RowHeaderSelect时,似乎没

在DataGridView中,按SHIFT键和空格键将默认选择整行。我在中找到的唯一解决方案是关闭行选择功能。虽然这样做有效,但并不理想,因为我仍然希望能够使用行选择器选择整行,例如,删除行,并且通过将SelectionMode属性更改为RowHeaderSelect以外的任何属性,我将失去这种能力。有没有一种方法可以将SHIFT+空格组合设为陷阱,并将其替换为简单空格?当控件的multiselect属性设置为True,SelectionMode属性设置为RowHeaderSelect时,似乎没有一个键事件能够识别该击键,因此我无法使用它们


ETA:我想也许关闭MultiSelect并将选择模式更改为CellSelect,然后为RowHeaderMouseClick事件添加一个事件处理程序会起作用……不。

在这里,这对我来说很好

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

只要对它们进行实验,我希望你能有所收获?

我发现实现这一点的最佳方法是从DataGridView继承并重写ProcessCmdKey方法。然后你可以截取Shift+空格,只发送空格。只需将此类添加到项目中,并将所有DataGridView切换到MyDataGridView即可。我的解决方案从这个问题中获得了灵感,这也解释了为什么Zaggler的解决方案不起作用,以及这篇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);
    }
}
这就是我的解决方案:

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

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

由于某些原因,按键事件从未触发。您是否在潜艇上设置断点以查看它是否击中它?还要确保您的对象名对于处理程序是正确的…是的,断点集,我使用IDE创建了存根,所以我知道它指向正确的控件和事件。想想看……这个表单是一个MDI子表单,它的父表单重写了ProcessCmdKey函数,但最后该函数将控制权返回给MyBase.ProcessCmdKey。这应该允许其他基于密钥的事件触发,对吗?