6

I've just noticed that DataGridViews have a default shortcut so that whenever you press Ctrl + H, the DataGridView's editing control backspaces, and can delete your entire selection within the cell.

This can get quite annoying since I want to open a Replace box whenever Ctrl + H is pressed. Is there any way to stop the backspacing while still being able to use it to open the replace box?

I'm running C# 2.0, but I could update my application to 3.5 if the newer C# has a solution.

Jasper
  • 2,166
  • 4
  • 30
  • 50
User2400
  • 2,373
  • 2
  • 20
  • 22

2 Answers2

5

This goes into your form code:

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{     
  if (keyData == (Keys.Control | Keys.H))
  {
    //ShowReplaceDialog() or whatever it is you want to do here.
    return true; //we handled the key
  }

  return base.ProcessCmdKey(ref msg, keyData); //we didn't handle it
}
szevvy
  • 3,446
  • 2
  • 18
  • 10
  • Thanks it works perfectly! Can you give me any pointers on when to override ProcessCmdKey instead of just handling a KeyDown event? – User2400 Apr 12 '10 at 05:23
  • Not sure, TBH. MSDN says: "This method is called during message preprocessing to handle command keys. Command keys are keys that always take precedence over regular input keys. Examples of command keys include accelerators and menu shortcuts." – szevvy Apr 12 '10 at 05:57
  • How to keep Ctrl + c and Ctrl +v functionality intact with this. I am not able to use these shortcuts for copy/paste. Please assist. – VJOY Oct 07 '11 at 04:47
0

void m_dgv_KeyDown(object sender, KeyEventArgs e)
    {
               if (e.KeyCode == (Keys.Control | Keys.H))
                {
                  e.Handled = true;
                }
   }
Bedasso
  • 1,652
  • 1
  • 14
  • 17