Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 136,511 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,740 people online right now. Registration is fast and FREE... Join Now!




Question about keyPress TextBox in C#

 
Reply to this topicStart new topic

Question about keyPress TextBox in C#, I need to check if the user presses enter

Dumpen
14 Aug, 2008 - 01:34 AM
Post #1

New D.I.C Head
*

Joined: 21 Mar, 2008
Posts: 40

Hi

How do I check if the user pressed "Enter" on the keyboard?

Right now I have a textbox with a keyPress event, but I cant seem to figure out how to do as my topic description says..

I think I need to use e.KeyChar, but cant seem to find it out

Thanks
User is offlineProfile CardPM
+Quote Post

born2c0de
RE: Question About KeyPress TextBox In C#
14 Aug, 2008 - 02:37 AM
Post #2

printf("I'm a %XR",195936478);
Group Icon

Joined: 26 Nov, 2004
Posts: 3,906



Thanked: 34 times
Dream Kudos: 2800
Expert In: 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

My Contributions
You can test for an Enter Keypress like this:

csharp

if( e.KeyChar == Keys.Enter)
{
//Enter Pressed
}
else
{
//Enter not pressed.
}

User is offlineProfile CardPM
+Quote Post

eclipsed4utoo
RE: Question About KeyPress TextBox In C#
14 Aug, 2008 - 04:42 AM
Post #3

D.I.C Regular
Group Icon

Joined: 21 Mar, 2008
Posts: 363



Thanked: 19 times
Dream Kudos: 25
My Contributions
QUOTE(born2c0de @ 14 Aug, 2008 - 06:37 AM) *

You can test for an Enter Keypress like this:

csharp

if( e.KeyChar == Keys.Enter)
{
//Enter Pressed
}
else
{
//Enter not pressed.
}



QUOTE
Operator '==' cannot be applied to operands of type 'char' and 'System.Windows.Forms.Keys'


the KeyPress event uses the char value of the keys. so you will either need to do this:

csharp

if (e.KeyChar == (char)Keys.Enter)
{
// Enter key pressed
}
else
{
// some other key pressed
}


or

csharp

if (e.KeyChar == 13) // 13 is the ASCII value for a carriage return
{
// Enter key pressed
}
else
{
// some other key pressed
}


User is offlineProfile CardPM
+Quote Post

Dumpen
RE: Question About KeyPress TextBox In C#
14 Aug, 2008 - 11:05 PM
Post #4

New D.I.C Head
*

Joined: 21 Mar, 2008
Posts: 40

Thanks, worked like a charm smile.gif
User is offlineProfile CardPM
+Quote Post

Dumpen
RE: Question About KeyPress TextBox In C#
15 Aug, 2008 - 12:56 AM
Post #5

New D.I.C Head
*

Joined: 21 Mar, 2008
Posts: 40

Is there a list of ascii key char values?

I need to know the values for return and delete
User is offlineProfile CardPM
+Quote Post

eclipsed4utoo
RE: Question About KeyPress TextBox In C#
15 Aug, 2008 - 04:26 AM
Post #6

D.I.C Regular
Group Icon

Joined: 21 Mar, 2008
Posts: 363



Thanked: 19 times
Dream Kudos: 25
My Contributions
QUOTE(Dumpen @ 15 Aug, 2008 - 04:56 AM) *

Is there a list of ascii key char values?

I need to know the values for return and delete


http://www.asciitable.com/

User is offlineProfile CardPM
+Quote Post

marcells23
RE: Question About KeyPress TextBox In C#
15 Aug, 2008 - 04:32 AM
Post #7

D.I.C Head
Group Icon

Joined: 22 Aug, 2007
Posts: 134



Thanked: 3 times
Dream Kudos: 125
My Contributions


Windows Key Enumeration
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 08:51PM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month