• 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
  aero9 said:

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
  aero9 said:

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

    • This was cool back in the day when done properly - loved having icons of specific devices.
    • Microsoft quietly burying a massive Windows 7 hardware driver feature as Windows 11 kills it by Sayan Sen Last month Microsoft announced a big update for Windows hardware drivers. The company declared that it was killing Windows Device metadata and the Windows Metadata and Internet Services (WMIS). For those wondering what it is, device metadata, as the name suggests, is the collection of additional, user-facing information that an original equipment manufacturer (OEM) provides about a hardware device. The feature was introduced with Windows 7 and can include stuff like icons, logos, descriptive texts, among other things, that help the Windows UI display details about such devices in places like Task Manager or Device Manager. This was a huge deal back in the day when Windows 7 debuted. The company called the feature "Device Stage" and Microsoft described it as a "new visual interface" that essentially worked like a "multi-function version of Autoplay where it displays all the applications, services, and information related to your device." It is often considered synonymous with the Windows "Devices and Printers" Control Panel applet. Neowin did an in-depth overview of the feature when it first launched which you can find in its dedicated article here. The Windows OS was able to obtain the device experience metadata from the WMIS, but now that the feature is being deprecated, Microsoft has begun removing information about Device Stage from its official support documents. Neowin noticed while browsing that a support article regarding automatic Windows hardware drivers was updated for Windows 11 and 10 sometime last year after the release of Windows 11 24H2. Previously, this article was geared for Windows 7 and was much longer. It also contained information about Device Stage, which, as mentioned above, was a headlining feature on Windows 7. In the said article, the section "If Windows can't find information about your device in Device Stage" has been deleted. You can find the archived version of the support page here. Aside from shortening the amount of information on the page, Microsoft has also added some more details on it. The company has now tried to define what the Microsoft Basic Display Adapter is, how updating drivers through Device Manager works, as well as a thorough and detailed troubleshooting section for common hardware driver errors on Windows, including one for USB-C. You can find all the new details on the updated support page here on Microsoft's website.
    • Sounds creepy to say the least. Don't need nor want AI having access to my history. They're claiming it to be an "offline" model now, but how can we guarantee they don't go behind our backs and change that?
    • Exactly! Without those fundamentals you've mentioned, Democracy is literally just Demonstration of Crazy, nothing to be proud of in such system.
    • Still I see almost no ads in mobile Edge unlike Chrome. So their browser is much better at blocking ads than Chrome and it is a fact. It even blocks ads on YouTube and you can add simple custom block filters. Also, Edge still support manifest v2 on desktop, so I'll look for another browser when I start seeing ads again.
  • Recent Achievements

    • First Post
      viraltui earned a badge
      First Post
    • Reacting Well
      viraltui earned a badge
      Reacting Well
    • Week One Done
      LunaFerret earned a badge
      Week One Done
    • Week One Done
      Ricky Chan earned a badge
      Week One Done
    • Week One Done
      maimutza earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      481
    2. 2
      +FloatingFatMan
      264
    3. 3
      snowy owl
      238
    4. 4
      ATLien_0
      234
    5. 5
      Edouard
      176
  • Tell a friend

    Love Neowin? Tell a friend!