Click here to Skip to main content
15,900,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Have been using google but with no success. Wenn i press "Enter" it run several times
and pops up plenty of "MessageBoxes"

this.TextBox.KeyPress += new System.Windows.Forms.KeyPressEventHandler(CheckEnter);


C#
private void CheckEnter(object sender, System.Windows.Forms.KeyPressEventArgs e)
{

    if (e.KeyChar == (char)13) // Enter
    {
        TextBox.Text = " CodeProject ";

        MessageBox.Show (" Enter pressed ");
    }

}
Posted
Updated 16-Nov-13 12:58pm
v3
Comments
Sergey Alexandrovich Kryukov 16-Nov-13 19:13pm    
Please see: I added the tag "System.Windows.Forms" for you. Not having this tag (or, in other cases, WPF, Silverlight, ASP.NET, etc., your UI library/framework or application type) is the permanent source of ambiguous questions. Your question was not ambiguous (thank you!), but proper tags are important. Someone looks at the list of questions and wants to decide to open your question page or not at all... I hope you see the point.
—SA
The Raising Programmer 17-Nov-13 6:41am    
This was my first post and I am grateful for the solutions and the information that was added. I will try to help the community with the knowledge I have.

If you need to respond to Enter key press, use different event:
C#
this.textBox.KeyDown += (sender, eventArgs) => {
   if (eventArgs.KeyCode == Keys.Enter; // it is neither 13 nor 10, please see below
};


You could use the event KeyUp the same way, not not very different event KeyPress which really works with characters and is, importantly, cancellable (and this is the way to filter some characters from input).

Please see:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.keydown%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.keyeventargs%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.keyeventargs.keycode%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.keys%28v=vs.110%29.aspx[^].

You need to understand that keys presses are lower level then characters (scan codes are even low level), and many do not correspond to any, others depends on current thread culture and current input method and input language chosen for a current application. If you need to handle the press of a key, do just this, don't try to type cast it to some character. Besides, the characters created by pressing Enter (say, in TextBox), depend on the platform (and CLR is actually multi-platform, not just Microsoft's):
http://en.wikipedia.org/wiki/End_of_line[^].

You also need to understand that in event-oriented programming nothing is "checked", instead, you are handling the event, and, in this case, the event triggered by hardware interrupt.

—SA
 
Share this answer
 
v2
In the case you want to allow keyboard-repeat for all characters except the 'Return key:
C#
private bool IsReturnDown = false;

public void textBox1_KeyUp(object sender, KeyEventArgs e)
{
    IsReturnDown = false;
}

public void textBox1_KeyDown(object sender, KeyEventArgs e)
{
    // SuppressKeyPress requires FrameWork >= 2.0
    e.SuppressKeyPress = IsReturnDown;

    IsReturnDown = e.KeyCode == Keys.Enter;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900