How do you disable a DataGridView's keyboard shorcuts?

百般思念 提交于 2019-12-19 07:50:51

问题


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.


回答1:


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
}



回答2:


void m_dgv_KeyDown(object sender, KeyEventArgs e)
    {
               if (e.KeyCode == (Keys.Control | Keys.H))
                {
                  e.Handled = true;
                }
   }


来源:https://stackoverflow.com/questions/2619535/how-do-you-disable-a-datagridviews-keyboard-shorcuts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!