Click here to Skip to main content
15,867,594 members
Articles / Programming Languages / C#
Article

Redirect the Enter key from a Windows Forms TextBox

Rate me:
Please Sign up or sign in to vote.
4.68/5 (13 votes)
24 Feb 20061 min read 126.5K   1.5K   45   8
A useful trick to customize the comportment of the Enter key in a Windows Forms TextBox control.

Sample Image - CustomizedTextBox.png

Introduction

Would you like to prevent the Enter key in a TextBox from automatically pressing the OK button of a Form, assuming you attached a OK button to the AcceptButton property of the Form?

The common way to redirect keys is to intercept their interpretation by attaching a method to the event handlers KeyPress or KeyDown of the TextBox control, or to derive from the class TextBox in order to override the methods OnKeyPress or OnKeyDown. Unfortunately, in this case, it fails: the redirection code is not interpreted and the OK button is automatically activated by the form itself. It happens because the parent form intercepts and stops the KeyPress and KeyDown signals. You don't get better results by overriding ProcessKeyPreview.

Instead, you have to inherit from TextBox and override the ProcessCmdKey virtual method.

The following C++ and C# code snippets show how to apply a tabulation when the user presses Enter, so that it switches to the next control.

Using the code

If you would like to use this code in your program, simply paste the class declaration in your code, and use CustomizedTextBox instead of System.Windows.Forms.TextBox in your code. If you want to enable the use of this control in the Forms Editor, then you should make a separate library project and import the contents of the DLL into the Visual Studio ToolBox.

The code in C#:

C#
//! Use instances of this class instead of TextBox
// in order to Redirect the process of the Enter Key,
// before the Form does it for you
public class CustomizedTextBox : System.Windows.Forms.TextBox 
{    
    // This method intercepts the Enter Key
    // signal before the containing Form does
    protected override bool ProcessCmdKey(ref 
              System.Windows.Forms.Message m, 
              System.Windows.Forms.Keys k) 
    {
        // detect the pushing (Msg) of Enter Key (k)
        if(m.Msg == 256 && k == 
               System.Windows.Forms.Keys.Enter) 
        {
            // Execute an alternative action: here we
            // tabulate in order to focus
            // on the next control in the formular
            System.Windows.Forms.SendKeys.Send("\t");
            // return true to stop any further
            // interpretation of this key action
            return true; 
        }
        // if not pushing Enter Key,
        // then process the signal as usual
        return base.ProcessCmdKey(ref m,k);
    }
}

The code in managed C++:

MC++
 //! Use instances of this class instead
// of TextBox in order to Redirect the process
// of the Enter Key, before the Form does it for you
public __gc class CustomizedTextBox : 
       public System::Windows::Forms::TextBox 
{
protected:
    // This method intercepts the Enter Key
    // signal before the containing Form does
    virtual bool ProcessCmdKey(System::Windows::Forms::Message 
                           * m, System::Windows::Forms::Keys k) 
    {
        // detect the pushing (Msg) of Enter Key (k)
        if(m->Msg == 256 && k == 
               System::Windows::Forms::Keys::Enter) 
        {
            // Execute an alternative action: here we
            // tabulate in order to focus
            // on the next control in the formular
            System::Windows::Forms::SendKeys::Send(S"\t");
            // return true to stop any further
            // interpretation of this key action
            return true; 
        }
        // if not pushing Enter Key,
        // then process the signal as usual
        return __super::ProcessCmdKey(m,k);
    }
};

Points of Interest

I hope this code will prevent some of you from wasting 3 or 4 hours looking for a trick that is not really (conceptually) interesting, but useful.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Germany Germany
Jerome RG studied Computer Science in France and Germany. He obtained a Master Degree in Computer Science of the Technical University of Berlin. In September 2005, he built with two colleagues a company developing IT Solutions for aeronautics.

Comments and Discussions

 
Suggestionversion with EnterKeyPressed event handler Pin
Dan Randolph26-Aug-12 12:02
Dan Randolph26-Aug-12 12:02 
GeneralMy vote of 5 Pin
Dan Randolph26-Aug-12 11:11
Dan Randolph26-Aug-12 11:11 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey12-Mar-12 21:51
professionalManoj Kumar Choubey12-Mar-12 21:51 
Questionenter key Pin
debasish_don7-Jan-12 4:54
debasish_don7-Jan-12 4:54 
GeneralYet another method Pin
xMotherx3-May-08 9:08
xMotherx3-May-08 9:08 
General[Message Deleted] Pin
alexreg-deleted25-Dec-06 1:14
alexreg-deleted25-Dec-06 1:14 
GeneralRe: Easiest Method Pin
MarkDoubson5-Nov-08 12:56
MarkDoubson5-Nov-08 12:56 
This is not a same action - if multilined textbox used AcceptReturn - press Enter key will produce extra line instead of needed Tab action
GeneralAlternative method Pin
Sidonath23-Mar-06 6:39
Sidonath23-Mar-06 6:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.