Discussion:
Esc in DataGridView on a dialog
(too old to reply)
Paul S
2008-04-15 13:55:02 UTC
Permalink
Hi

I have a DataGridView on a dialog. The dialog has a Cancel button abd the
CancelButton property set to this button so pressing ESC in the dialog closes
the dialog.
The grid has a drop down box in one of the cells. When the user regrets a
selection he presses ESC to restore the previous value. However this closes
the dialog. Is there a way so that pressing ESC when the dropdown box is in
edit mode restores the old value. When not in Edit mode Esc should close the
dialog.
--
Thansk
Paul S
Linda Liu[MSFT]
2008-04-16 07:00:01 UTC
Permalink
This post might be inappropriate. Click to display it.
Paul S
2008-04-16 14:26:02 UTC
Permalink
Just what I needed
--
Thanks Linda
Paul S
Post by Linda Liu[MSFT]
Hi Paul,
When a DataGridView control gets focused and the user presses the ESC key,
the DataGridView exits from the edit mode if it was in edit mode and then
passes the ESC key to the form.
To get what you want, you need to prevent the ESC key from being passed to
the form in the above case.
To do this, derive a new class from the DataGridView class and override the
ProcessDialogKey method. If the current cell is in edit mode, call the
ProcessEscapeKey method to process the ESC key and return true directly;
otherwise, call the base.ProcessDialogKey method.
class MyDataGridView : DataGridView
{
protected override bool ProcessDialogKey(Keys keyData)
{
if (keyData == Keys.Escape)
{
if (this.CurrentCell.IsInEditMode)
{
this.ProcessEscapeKey(Keys.Escape);
return true;
}
}
// If the keyData is Keys.Escape, the base.ProcessDialogKey
method will pass the ESC key to the form.
return base.ProcessDialogKey(keyData);
}
}
Use the derived DataGridView instead of the standard DataGridView on your
form.
If you have any question, please feel free to let me know.
Sincerely,
Linda Liu
Microsoft Online Community Support
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
km
2008-06-11 15:44:03 UTC
Permalink
This post might be inappropriate. Click to display it.
Denis Abramov
2011-08-11 16:48:33 UTC
Permalink
Just for completeness sake:

''' <summary>
''' Handle the Dialog "Enter" and "ESC" keys properly
''' </summary>
''' <param name="keyData"></param>
''' <returns></returns>
''' <remarks></remarks>
Protected Overrides Function ProcessDialogKey(keyData As System.Windows.Forms.Keys) As Boolean
If CurrentCell.IsInEditMode Then
If keyData = Keys.Escape Then
Me.ProcessEscapeKey(keyData)
Return True
ElseIf keyData = Keys.Enter Then
Me.ProcessEnterKey(keyData)
Return True
End If
Else
'If the keyData is Keys.Escape, Keys.Enter, etc. the base.ProcessDialogKey method will pass the ESC key to the form
Return MyBase.ProcessDialogKey(keyData)
End If

End Function
End Class
Post by Paul S
Hi
I have a DataGridView on a dialog. The dialog has a Cancel button abd the
CancelButton property set to this button so pressing ESC in the dialog closes
the dialog.
The grid has a drop down box in one of the cells. When the user regrets a
selection he presses ESC to restore the previous value. However this closes
the dialog. Is there a way so that pressing ESC when the dropdown box is in
edit mode restores the old value. When not in Edit mode Esc should close the
dialog.
--
Thansk
Paul S
Post by Linda Liu[MSFT]
Hi Paul,
When a DataGridView control gets focused and the user presses the ESC key,
the DataGridView exits from the edit mode if it was in edit mode and then
passes the ESC key to the form.
To get what you want, you need to prevent the ESC key from being passed to
the form in the above case.
To do this, derive a new class from the DataGridView class and override the
ProcessDialogKey method. If the current cell is in edit mode, call the
ProcessEscapeKey method to process the ESC key and return true directly;
otherwise, call the base.ProcessDialogKey method.
class MyDataGridView : DataGridView
{
protected override bool ProcessDialogKey(Keys keyData)
{
if (keyData == Keys.Escape)
{
if (this.CurrentCell.IsInEditMode)
{
this.ProcessEscapeKey(Keys.Escape);
return true;
}
}
// If the keyData is Keys.Escape, the base.ProcessDialogKey
method will pass the ESC key to the form.
return base.ProcessDialogKey(keyData);
}
}
Use the derived DataGridView instead of the standard DataGridView on your
form.
If you have any question, please feel free to let me know.
Sincerely,
Linda Liu
Microsoft Online Community Support
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
Post by Paul S
Just what I needed
--
Thanks Linda
Paul S
Post by km
1. Handle the EditCtrlShowing event for the grid. In it, attach your
private void OnEditCtrlShowing(object
sender,DataGridViewEditingControlShowingEventArgs e)
{
e.Control.PreviewKeyDown -=
new PreviewKeyDownEventHandler(OnPreviewKeyDown);
e.Control.PreviewKeyDown +=
new PreviewKeyDownEventHandler(OnPreviewKeyDown);
}
2. In your handler for the edit contro'ls PreviewKeyDown event, check for
private void OnPreviewKeyDown(object sender,PreviewKeyDownEventArgs e)
{
if(e.KeyCode==Keys.Escape)
{
m_oGrid.CancelEdit();
m_oGrid.EndEdit();
}
}
If this is a bad approach I'd welcome an explanation as to why, but so far I
haven't found any problems with it.
Note: Framework documentation not withstanding, CancelEdit doesn't cancel
the "edit mode" but seems to undo the edit and leave the control in the edit
mode.
Loading...