2012-12-26 21 views
4

在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

回答

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

由於某些原因,KeyDown事件不會觸發。 – mounty

+0

你是否在子上設置了一個斷點以查看它是否正在擊中它?還要確保你的對象名稱對於處理程序是正確的... – Codexer

+0

是的,斷點集,我用IDE來創建存根,所以我知道它指向正確的控制權和正確的事件。只是想到了一些......這種形式是一個MDI子代,它的父代形式覆蓋了'ProcessCmdKey'函數,但最後該函數將控制權返回給'MyBase.ProcessCmdKey'。這應該允許其他基於關鍵事件觸發,對吧? – mounty

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); 
    } 
}