选中列表框项时,在CheckedListBox控件中获取关键事件

时间:2011-08-18 15:49:08

标签: c# winforms

我使用CheckedListBox作为自定义DataGridView单元类编辑模式的基础。我想在用户按下转义键时关闭编辑。如果用户没有选择覆盖OnKeyDown,则可以完美地工作。问题是,如果他们做OnKeyDown(和OnKeyPressed)不被CheckedListBox触发;而是行项取消选择自己并吃掉事件。这意味着如果用户通过单击列表中的任何项目进行选择,则需要两次按下转义才能关闭编辑。一个取消选择他们点击的行,另一个取消编辑模式。

2 个答案:

答案 0 :(得分:2)

尝试覆盖自定义DataGridView本身的IsInputKey,如果是[Esc]则返回false,因此网格忽略Esc。

http://msdn.microsoft.com/en-us/library/system.windows.forms.control.isinputkey.aspx

如果在“编辑模式”或类似内容中没有任何内容,则只能忽略inputKey,这可能会导致此行为。

答案 1 :(得分:0)

为了访问转义键,我必须更改IDataGridViewEditingControl.EditingControlWantsInputKey(相当于普通控件上的IsInputKey)的行为,以将转义传递给我的OnKeyDown事件。

/// <summary>
/// Implements the IDataGridViewEditingControl.EditingControlWantsInputKey method.
/// </summary>
/// <param name="key"></param>
/// <param name="dataGridViewWantsInputKey"></param>
/// <returns></returns>
public bool EditingControlWantsInputKey(Keys key, bool dataGridViewWantsInputKey)
{
    // Let the custom edit control handle the keys listed.
    switch (key & Keys.KeyCode)
    {
        case Keys.Escape:
            return true;
        default:
            return !dataGridViewWantsInputKey;
    }
}