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.

Copy Code
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] -

Thursday, December 06, 2007 3:30:45 AM (Romance Standard Time, UTC+01:00)
Sorry! I do not quite understand it, you can say some more detail?
Wednesday, April 09, 2008 8:44:20 PM (Romance Standard Time, UTC+01:00)
Thanks for the post. It works great.
Michael Wheeler
Wednesday, April 30, 2008 2:12:56 AM (Romance Standard Time, UTC+01:00)
ccc
Name
E-mail
Home page

Comment (HTML not allowed)  

Enter the code shown (prevents robots):

Navigation
Archive
<November 2008>
SunMonTueWedThuFriSat
2627282930311
2345678
9101112131415
16171819202122
23242526272829
30123456
About the author/Disclaimer

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

© Copyright 2008
Christoph De Baene
Sign In
Statistics
Total Posts: 151
This Year: 22
This Month: 1
This Week: 0
Comments: 147
All Content © 2008, Christoph De Baene
DasBlog theme 'Business' created by Christoph De Baene (delarou)