sharing about .NET and technology RSS 2.0
# Thursday, February 22, 2007

In many enterprise applications there is the need that, regardless on which control you have the focus, that you can hit the 'Enter' and/or 'Esc' key to perform a default action. This behaviour is also common to web applications. On the Form control you find properties like AcceptButton and CancelButton, whereas the UserControl doesn't have these properties. The code below contains an AcceptButton and CancelButton that allows you to define a default action when the Enter or Esc key is pressed respesctively.

public class UserControlEx : System.Windows.Forms.UserControl
{    
    private Button _acceptButton;
    private Button _cancelButton;

    public event EventHandler<EventArgs> AcceptEvent;
    public event EventHandler<EventArgs> CancelEvent;

    [Browsable(true)]
    public Button AcceptButton
    {
        get { return _acceptButton; }
        set { _acceptButton = value; }
    }

    [Browsable(true)]
    public Button CancelButton
    {
        get { return _cancelButton; }
        set { _cancelButton = value; }
    }

    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
        if (msg.WParam.ToInt32() == (int)Keys.Enter)
        {
            OnAcceptEvent(EventArgs.Empty);

            if (_acceptButton != null)
                _acceptButton.PerformClick();
        }

        if (msg.WParam.ToInt32() == (int)Keys.Escape)
        {
            OnCancelEvent(EventArgs.Empty);

            if (_cancelButton != null)
                _cancelButton.PerformClick();
        }

        return base.ProcessCmdKey(ref msg, keyData);
    }

    protected virtual void OnAcceptEvent(EventArgs args)
    {
        if (AcceptEvent != null)
            AcceptEvent(this, args);
    }

    protected virtual void OnCancelEvent(EventArgs args)
    {
        if (CancelEvent != null)
            CancelEvent(this, args);
    }
}
Thursday, February 22, 2007 10:32:31 PM (Romance Standard Time, UTC+01:00) -  # -  Comments [3] -
.NET | Windows Forms
Wednesday, April 09, 2008 9:44:20 PM (Romance Daylight Time, UTC+02:00)
Thanks for the post. It works great.
Michael Wheeler
Thursday, February 26, 2009 3:03:31 PM (Romance Standard Time, UTC+01:00)
We used this method for our usercontrols with success, thanks!

One correction to make the functionality fall better in line with System.Windows.Forms.Form and to avoid unintended consequences -- the ProcessCmdKey override should only call PerformClick on a button if it is Enabled.
Thursday, April 02, 2009 5:36:24 PM (Romance Daylight Time, UTC+02:00)
Thanks! Converted it to VB.net and used Infragistics controls (our standard):

Private _acceptButton As UltraButton
Private _cancelButton As UltraButton

Public Event AcceptEvent(ByVal e As System.EventArgs)
Public Event CancelEvent(ByVal e As System.EventArgs)

<Browsable(True)> _
Public Property AcceptButton() As UltraButton
Get
Return _acceptButton
End Get
Set(ByVal value As UltraButton)
_acceptButton = value
End Set
End Property

<Browsable(True)> _
Public Property CancelButton() As UltraButton
Get
Return _cancelButton
End Get
Set(ByVal value As UltraButton)
_cancelButton = value
End Set
End Property

Protected Overrides Function ProcessCmdKey(ByRef msg As Message, ByVal keyData As Keys) As Boolean
If (msg.WParam.ToInt32() = Keys.Enter) Then
RaiseEvent AcceptEvent(EventArgs.Empty)

If (_acceptButton IsNot Nothing) AndAlso _acceptButton.Enabled Then
_acceptButton.PerformClick()
End If
End If

If (msg.WParam.ToInt32() = Keys.Escape) Then
RaiseEvent CancelEvent(EventArgs.Empty)

If (_cancelButton IsNot Nothing) AndAlso _cancelButton.Enabled Then
_cancelButton.PerformClick()
End If
End If

Return MyBase.ProcessCmdKey(msg, keyData)
End Function
John Fisera
OpenID
Please login with either your OpenID above, or your details below.
Name
E-mail
(will show your gravatar icon)
Home page

Comment (Some html is allowed: a@href@title, b, blockquote@cite, em, i, strike, strong, sub, sup, u) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.  

Enter the code shown (prevents robots):

Live Comment Preview
Navigation
Archive
<September 2010>
SunMonTueWedThuFriSat
2930311234
567891011
12131415161718
19202122232425
262728293012
3456789
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2010
Christoph De Baene
Sign In
Statistics
Total Posts: 176
This Year: 2
This Month: 0
This Week: 0
Comments: 283
All Content © 2010, Christoph De Baene
DasBlog theme 'Business' created by Christoph De Baene