Jump to content



Photo

[C#] how do i detect enter key in a textbox ?


  • This topic is locked This topic is locked
14 replies to this topic

#1 aero9

aero9

    Neowinian

  • Joined: 15-May 04
  • Location: Bharat

Posted 19 January 2006 - 13:25

how do i detect if the user has just pressed the enter (return) key ?



and also are there events like mouse move/enter/leave for a text box ?
whats the function prototype for them ?



thanks


#2 vetJohn

John

    Neowinian Senior

  • Joined: 28-January 02
  • Location: Des Moines, IA

Posted 19 January 2006 - 14:49

Do you want to detect a keystroke on the enter key? Or are you looking to recognize a newline character?

#3 yyy

yyy

    Neowinian

  • Joined: 15-July 04

Posted 19 January 2006 - 14:51

You need to add a KeyPress event to your text box control:

this.textBox1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(CheckKeys);

Then check which key was entered in the handler function:

private void CheckKeys(object sender, System.Windows.Forms.KeyPressEventArgs e)
		{
			if (e.KeyChar == (char)13)
			{
			   // Then Enter key was pressed
			}
		}

(char)13 means the Enter key.

Yes - the text box control has mouse move/enter/leave events.

#4 OP aero9

aero9

    Neowinian

  • Joined: 15-May 04
  • Location: Bharat

Posted 19 January 2006 - 17:10

perfect thanks yyy

#5 OP aero9

aero9

    Neowinian

  • Joined: 15-May 04
  • Location: Bharat

Posted 24 January 2006 - 03:06

hey yyy,
i dont know why but this is going through though the "// Then Enter key was pressed" part ==> [the number of charecters in the textbox] + 1 times

so for example if i have "hello" and i press enter it would run that part of the code 5+1 times.

how do i fix it ?

#6 dannyres

dannyres

    Neowinian

  • Joined: 22-September 02

Posted 24 January 2006 - 07:03

Hmm thats wierd, are you sure you arnt accidentally adding 5 event handlers by calling whatever method you add the eventhandlers in? Also you should use the ProcessCmdKey method IIRC to detect the enter key properly. :)

EDIT: just checked up on ProcessCmdKey and it really depends on the situation weather to use it.. if your wanting to close a dialog or accept something then you should problably be using ProcessCmdKey.. could you explain your situation a little more? :)

EDIT again: Looks like I misuderstood the problem a little.. the KeyPress event is raised with any key you press.. so you need to make sure your only running the code you want for the enter key inside an if statement.

Edited by dannyres, 24 January 2006 - 07:12.


#7 yyy

yyy

    Neowinian

  • Joined: 15-July 04

Posted 24 January 2006 - 10:13

hey yyy,
i dont know why but this is going through though the "// Then Enter key was pressed" part ==> [the number of charecters in the textbox] + 1 times

so for example if i have "hello" and i press enter it would run that part of the code 5+1 times.

how do i fix it ?

You're probably doing something wrong. I tested this code and it works fine. Maybe there's something else in your code that causes the problem.

#8 OP aero9

aero9

    Neowinian

  • Joined: 15-May 04
  • Location: Bharat

Posted 24 January 2006 - 16:47

private void textBox1_TextChanged(object sender, System.EventArgs e)
{
this.toolTip1.SetToolTip(this.PictureBox2, textBox1.Text);
this.toolTip1.SetToolTip(this.textBox1, textBox1.Text);
this.textBox1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(CheckKeys);
}




private void CheckKeys(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if (e.KeyChar == (char)13)
{ i=i+1;
numericUpDown1.Value=i;
//PictureBox2_Click(sender,e);
}
}







that's my code, now lets say i type in "hello" and press enter the value of the numericupdown box changes from 0 to 5, enter again.... 10 again.... 15 and so on...., u see any thing wrong ?

#9 yyy

yyy

    Neowinian

  • Joined: 15-July 04

Posted 24 January 2006 - 19:42

I can't figure out the problem from only this code - maybe you change the value of i somewhere else in your code? Maybe in the PictureBox2_Click() function?

#10 azcodemonkey

azcodemonkey

    Neowinian Senior

  • Joined: 19-March 02

Posted 25 January 2006 - 01:02

private void textBox1_TextChanged(object sender, System.EventArgs e)
{
this.toolTip1.SetToolTip(this.PictureBox2, textBox1.Text);
this.toolTip1.SetToolTip(this.textBox1, textBox1.Text);
this.textBox1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(CheckKeys);
}
private void CheckKeys(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if (e.KeyChar == (char)13)
{ i=i+1;
numericUpDown1.Value=i;
//PictureBox2_Click(sender,e);
}
}
that's my code, now lets say i type in "hello" and press enter the value of the numericupdown box changes from 0 to 5, enter again.... 10 again.... 15 and so on...., u see any thing wrong ?


You're adding the event handler everytime the text changes. Set the handler once in the InitializeComponent method on the form.

#11 vetJohn

John

    Neowinian Senior

  • Joined: 28-January 02
  • Location: Des Moines, IA

Posted 25 January 2006 - 03:25

You should look into Event Driven Programming and understand how events work, what event handlers do and when they are called, and how to correctly use event handlers. I think you think they're just simple functions by the way you're using them, and they're not.

#12 OP aero9

aero9

    Neowinian

  • Joined: 15-May 04
  • Location: Bharat

Posted 25 January 2006 - 09:10

You're adding the event handler everytime the text changes. Set the handler once in the InitializeComponent method on the form.



you're right, that fixed it.
cheers!

john. ill read up to understand it better, thanks

#13 yyy

yyy

    Neowinian

  • Joined: 15-July 04

Posted 25 January 2006 - 10:21

Oh, yes - I didn't notice that you add it in the Text Changed event handler function.

#14 OP aero9

aero9

    Neowinian

  • Joined: 15-May 04
  • Location: Bharat

Posted 25 January 2006 - 12:59

check this out....
http://www.neowin.ne...howtopic=424942

#15 wastier

wastier

    Resident One Post Wonder

  • Joined: 02-October 08

Posted 02 October 2008 - 17:22

private void textBox1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
if (e.KeyValue == 13)
// Then Enter key was pressed
}