【问题标题】:How to allow my control to detect the Escape key before the DataGridView close my control?如何让我的控件在 DataGridView 关闭我的控件之前检测到 Escape 键?
【发布时间】:2010-10-01 07:37:34
【问题描述】:

我有一个带有撤销功能的控件,当用户按下 Escape 时,该控件将恢复原始值。

问题是当我将控件集成到 DataGridView 时。 DataGridView “吃掉” Escape 键,因此我的控件无法检测到 Escape 键。

当我在 EditingControlWantsInputKey 上设置“return true”时,我的控件能够检测到 Escape 键,但出现了其他问题,DataGridView 无法关闭我的控件,它停留在 EditMode。

如何让我的控件检测到 Escape 键同时还允许 DataGridView 关闭我的控件?

【问题讨论】:

    标签: datagridview escaping undo


    【解决方案1】:

    我能够解决我自己的问题。我公开了我的 LookupBoxUndo 方法,然后在我的 DataGridView 控件(class DgvLookupBoxEditingControl : LookupBox, IDataGridViewEditingControl)上,我输入了以下代码:

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == Keys.Escape)            
                this.Undo();            
    
            return base.ProcessCmdKey(ref msg, keyData);
    
    
        }
    

    【讨论】:

      【解决方案2】:

      只有在 Keys.KeyCode == Keys.Escape; 时才应该“返回 true”;
      否则返回 !dataGridViewWantsInputKey。

      【讨论】:

      • 我试过了,但没用。然后是我的控件吃掉了密钥,因此 DataGridView 无法关闭我的控件。
      【解决方案3】:

      或者您可以将 PreviewKeyDown 处理程序添加到您的编辑控件并在那里检测 Escape。

                  dataGridView1.EditingControlShowing += (o, e) => {
      
                  if(e.Control is DataGridViewTextBoxEditingControl)
                  {                 
                      var editBox = e.Control as DataGridViewTextBoxEditingControl;
                      editBox.PreviewKeyDown += KeyPressHandler;
                  }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-10-22
        • 1970-01-01
        相关资源
        最近更新 更多