在DataGridView中,按SHIFT和SPACE將默認選擇整行。我發現的唯一解決方案(參考vb.net DataGridView - Replace Shortcut Key with typed character)是關閉行選擇功能。雖然這是有效的,但這並不理想,因爲我仍然希望能夠使用行選擇器來選擇整行(例如,刪除行),並且通過將SelectionMode
屬性更改爲RowHeaderSelect
以外的任何其他屬性,我失去了該能力。有沒有一種方法可以捕捉SHIFT + SPACE組合並用簡單的SPACE替換它?似乎沒有任何關鍵事件甚至認識到,當控件的MutiSelect
屬性設置爲True
並且SelectionMode
屬性設置爲RowHeaderSelect
時,按鍵操作無法使用,因此我無法使用這些按鍵。 ETA:我想也許關閉MultiSelect
並將選擇模式更改爲CellSelect
,然後爲事件添加事件處理程序將工作...不行。重寫DataGridView Shift + Space
4
A
回答
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
只是實驗與他們希望的東西的作品適合您嗎?
0
我想出瞭如何完成此操作的最佳方式是從DataGridView繼承並重寫ProcessCmdKey方法。然後你可以截取Shift + Space併發送給Space。只需將此類添加到您的項目中,並將所有DataGridView切換到MyDataGridView。我的解決方案從這個DataGridView keydown event not working in C# SO問題中得到靈感(這也解釋了爲什麼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);
}
}
相關問題
- 1. MacVim在優勝美地:Shift-Space
- 2. C#:在datagridview中獲取行(shift + tab)
- 3. C#datagridview行選擇與CTRL和SHIFT
- 4. 是否可以在SQL Server Management Studio 2008中禁用'shift + space'快捷鍵?
- 5. C#:重寫DataGridView ScrollBars(自定義OnPaint)?
- 6. 寫從DataGridView
- 7. Caeser Shift Output
- 8. Memo1.Loadfromfile()space
- 9. Smartgit -Xignore-space-change?
- 10. split malloc mem space
- 11. 需要使用Space
- 12. DataGridView行重複
- 13. 重置DataGridView
- 14. VB.Net檢測datagridview編輯控件上的SHIFT鍵
- 15. 處理Shift +刪除
- 16. JSLint'Unexpected'(space)'錯誤?
- 17. Boost Tokenizer:Extra Space?
- 18. Cloud Foundry Org Space
- 19. Space Invaders ArrayList IndexOutOfBoundsException
- 20. java.lang.OutOfMemoryError:PermGen space + java
- 21. item without additional space
- 22. Teradata space up issues
- 23. Permgen space error tomcat
- 24. JSON.stringify(value [,replacer [,space]])
- 25. White Space/Coldfusion
- 26. Tabs vs Space indentation
- 27. SendKeys ALT + SPACE
- 28. NSMenuItem KeyEquivalent「」(space)bug
- 29. normalize-space(。)和normalize-space(text())之間的區別是什麼?
- 30. DataGridView緩慢重繪
由於某些原因,KeyDown事件不會觸發。 – mounty
你是否在子上設置了一個斷點以查看它是否正在擊中它?還要確保你的對象名稱對於處理程序是正確的... – Codexer
是的,斷點集,我用IDE來創建存根,所以我知道它指向正確的控制權和正確的事件。只是想到了一些......這種形式是一個MDI子代,它的父代形式覆蓋了'ProcessCmdKey'函數,但最後該函數將控制權返回給'MyBase.ProcessCmdKey'。這應該允許其他基於關鍵事件觸發,對吧? – mounty