Question

I have a control with an undo feature, when the user press Escape the control will revert the original value.

The problem is when I integrated my control to DataGridView. The DataGridView "eats" the Escape key, hence my control cannot detect the Escape key.

When I put "return true" on EditingControlWantsInputKey, my control was able to detect the Escape key, but other problem arised, the DataGridView cannot close my control, it stays in EditMode.

How to allow my control to detect the Escape key while also allowing the DataGridView to close my control?

Was it helpful?

Solution

I was able to solved my own problem. I made the Undo method of my LookupBox public, then on my DataGridView control (class DgvLookupBoxEditingControl : LookupBox, IDataGridViewEditingControl), I put the following code:

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

        return base.ProcessCmdKey(ref msg, keyData);


    }

OTHER TIPS

You should "return true" only when Keys.KeyCode == Keys.Escape;
otherwise return !dataGridViewWantsInputKey.

Or you can add PreviewKeyDown handler to your editing control and detect Escape there.

            dataGridView1.EditingControlShowing += (o, e) => {

            if(e.Control is DataGridViewTextBoxEditingControl)
            {                 
                var editBox = e.Control as DataGridViewTextBoxEditingControl;
                editBox.PreviewKeyDown += KeyPressHandler;
            }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top