vb.net – 覆盖DataGridView Shift Space

vb.net – 覆盖DataGridView Shift Space,第1张

概述在DataGridView中,按SHIFT和SPACE将默认选择整行.我发现的唯一解决方案(在 vb.net DataGridView – Replace Shortcut Key with typed character处引用)是关闭行选择功能.虽然这有效,但它并不理想,因为我仍然希望能够使用行选择器选择整行(例如,删除行),并通过将SelectionMode属性更改为除RowHeaderSel 在DataGrIDVIEw中,按SHIFT和SPACE将默认选择整行.我发现的唯一解决方案(在 vb.net DataGridView – Replace Shortcut Key with typed character处引用)是关闭行选择功能.虽然这有效,但它并不理想,因为我仍然希望能够使用行选择器选择整行(例如,删除行),并通过将SelectionMode属性更改为除RowheaderSelect以外的任何内容而失去该能力.有没有办法只捕获SHIFT SPACE组合并用简单的SPACE替换它?似乎没有任何关键事件甚至在控件的MutiSelect属性设置为True并且SelectionMode属性设置为RowheaderSelect时识别出击键,因此我无法使用它们.

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

解决方法 我想出如何实现这一点的最好方法是从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);    }}
总结

以上是内存溢出为你收集整理的vb.net – 覆盖DataGridView Shift Space全部内容,希望文章能够帮你解决vb.net – 覆盖DataGridView Shift Space所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1250418.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-07
下一篇 2022-06-07

发表评论

登录后才能评论

评论列表(0条)

保存