• 0

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


Question

14 answers to this question

Recommended Posts

  • 0

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.

  • 0

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 ?

  • 0

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
  • 0

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.

  • 0

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 ?

  • 0

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.

  • 0

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.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • Web search has been one of those crap ware that I try to get rid of asap. It's annoying and slow. It was a cheap way for Microsoft to drive traffic to Bing.
    • All I can say is that think the USB G8 is better in many ways, and costs the same or slightly less. I wouldn't part with mine. It's even a big improvement over the G6 USB. (You can also use studio phones (up to 600 OHMs I think) with the G8, but I have to say that even relatively cheap 30-50 OHM phones sound absolutely great--far better than through most internal motherboard sound!) Considering the difference in these products I am surprised that Creative is still making the PCIe internal card versions. https://www.headphoneer.com/cr...ve-sound-blaster-g8-review/
    • I missed this when you first posted it, but those are awesome icons! I would normally prefer things to be uniform, but each of those icons seems to fit perfectly for the game! If I may ask though, what was your thought process on which icons are circular - MK makes sense - and which ones should be rectangular or square?
    • What I like about Paint is using it almost exclusively for cropping and resizing images I get elsewhere--it's quick, easy and cheap... I keep it glued to my taskbar, in fact. Also, the clipping tool comes in handy, as well (hit print scrn on the keyboard and it activates immediately.)
    • I still remember it fondly today. It was so cool to work in 64-color Half Bright mode and 4.096-color HAM mode (interlaced) when x86 was still in 4-color CGA or 16-color EGA low res. C= never realized what it had until it was far too late--the failure of C= was the failure of its top management. The C= Amiga was 20 years ahead of its time, I always thought. It didn't hurt that in only 512k of chip memory, the Amiga could preemptively multitask when Apple was still doing gray scale graphics on tiny screens and along with everyone else was doing cooperative multitasking (running more than one app at a time in resident memory, but you could only run one of them at a time--had to manually switch between them.) I had a ball with AREXX scripting running between programs that had AREXX ports so that when you sent other applications data and instructions, those running applications could process the same in real time to output! Memories...
  • Recent Achievements

    • Dedicated
      Almohandis earned a badge
      Dedicated
    • Dedicated
      JuvenileDelinquent earned a badge
      Dedicated
    • First Post
      DrWankel earned a badge
      First Post
    • Reacting Well
      DrWankel earned a badge
      Reacting Well
    • Week One Done
      Supreme Spray LV earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      507
    2. 2
      +Edouard
      185
    3. 3
      PsYcHoKiLLa
      84
    4. 4
      Michael Scrip
      78
    5. 5
      Steven P.
      76
  • Tell a friend

    Love Neowin? Tell a friend!