处理Shift + Del

时间:2013-02-12 04:38:09

标签: c# winforms datagridview

我在DataGridView中有一个winform,其中包含delete,用于验证和删除这些行。但是,按 Shift + Del 会删除UI中的行。我尝试通过处理 boolean 事件来设置IsShiftPressed keydown ,但这看起来很难看。当然应该有更好的方法来做到这一点。

2 个答案:

答案 0 :(得分:2)

试试KeyUp Event

if (e.KeyCode == Keys.Del && e.Shift)
{
   if (dataGridView1.SelectedRows.Count > 0)
   {
      //Do Stuff
   }
   e.Handled = true;
}

答案 1 :(得分:1)

您可以尝试trap the control in C# according to this article.

class MyDataGrid : System.Windows.Forms.DataGrid
{
   protected override bool ProcessCmdKey(ref Message msg, Keys keyData) 
   {
            const int WM_KEYDOWN = 0x100;
            const int WM_SYSKEYDOWN = 0x104;

            if ((msg.Msg == WM_KEYDOWN) || (msg.Msg == WM_SYSKEYDOWN))
            {
                switch (keyData)
                {
                    case Keys.Shift | Keys.Delete:
                        MessageBox.Show("shift + del pressed");
                        break;
                }
            }
            return base.ProcessCmdKey(ref msg, keyData);
   }
}