• 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

    • Anno 117: Pax Romana gets a November release date as Ubisoft unveils Governor's Edition by Pulasthi Ariyasinghe Ubisoft's long-running city-building and management franchise, Anno, was first revealed to be receiving another entry back in 2024. While it has taken some time, Anno 117: Pax Romana finally received a firm release date today during the 2025 PC Gaming Show event just as pre-orders open up. Catch the latest cinematic trailer above. Anno 117: Pax Romana lands on November 13, 2025, letting fans loose on the Roman settlement-building venture. The game's first gameplay was revealed just a few weeks ago as well, which you can watch by heading here. As new features, the title is adding a province selection mechanic at the start of each game, a religion system, a research tree, land combat, modular shipbuilding, and, most importantly, diagonal roads and building construction. Those who pre-order the game will also receive a Builder Pack from today. This will carry the Wolf player sigil, a matching battle standard, the Town Crier statue, as well as the Capitoline Wolf statue. At the same time, a Gold Edition is available for pre-order that bundles the Year 1 Pass with the base game. This will carry access to three DLC packs, and judging by the teaser image, it looks like players will be heading to Egypt for new adventures as part of the expanded content. For fans who may want something a little more comprehensive, Ubisoft also unveiled the Anno 117 Governor’s Edition. This special edition comes with these collector's items and digital goodies: Collector's items: Amphitheatre 3D Puzzle (36 x 31 x 13 cm) 84-page Artbook featuring concept art and behind-the-scenes content - cover design elected by community! Forged Anno Symbol (approx. 7 cm) Albion & Latium Coins Steelbook® case - design elected by community! Town Crier's Letter (21 x 30 cm) Tesserae Works Blueprint (42 x 59 cm) 3 Lithographs (30 x 15 cm) Digital Content: Base Game Year 1 Pass, including: 3 upcoming DLCs Additional in-game content The Builder Pack: 3 exclusive ornaments 1 player sigil Anno 117: Pax Romana is slated to hit Steam, Ubisoft Connect, Epic Games Store, Xbox Series X|S, and PlayStation 5 platforms. Pre-orders are now available starting at $59.99 for the standard edition. Ubisoft+ subscribers will also receive the title as a day-one drop. In addition to the base game, members will also receive access to the upcoming DLC packs at launch for no extra cost.
    • Once the first went to a full version number, some just egregiously long numbers with every iteration of compilations/dates/times... what have you, it was a landslide. Long gone are the days of IE version 6, 7... now it's Edge 137.0.3296.16. So, why not iOS 2026.18.8778.322.10.800? Shocked that Edge isn't 2025.137.0.3296.16... seriously. When Samsung went from S10 to S11, to S20... to S21... I thought they were just doing new iterations of the same/similar models, then the next would have logically been S30/S31... but nope. Oh well... better things to think about then version numbers, leave that to the devs.
    • The problem -- as pointed to by the PowerShell output I quoted above -- was that for some reason it couldn't download the NuGet provider. Here's what did work: (1) Download the raw nupkg file (set-inetpubfolderacl.1.0.0.nupkg) from https://www.powershellgallery....s/Set-InetpubFolderAcl/1.0; (2) Extract (e.g., via 7-Zip) the Set-InetpubFolderAcl.ps1 file into C:\Program Files\WindowsPowerShell\Scripts; (3) Run the script via PowerShell as described in the quite thorough instructions given at https://www.windowslatest.com/...t-on-windows-11-windows-10/ . That worked for me, or seemed to. Oh, relief!
    • I like the show's/comic's violence being represented here, as so many others avoid it. Timing is key with this release due to the Marvel Tokon title.
  • Recent Achievements

    • First Post
      George Almeyda earned a badge
      First Post
    • Reacting Well
      BlakeBringer earned a badge
      Reacting Well
    • Reacting Well
      Lazy_Placeholder earned a badge
      Reacting Well
    • Dedicated
      Epaminombas earned a badge
      Dedicated
    • Veteran
      Yonah went up a rank
      Veteran
  • Popular Contributors

    1. 1
      +primortal
      469
    2. 2
      +FloatingFatMan
      265
    3. 3
      ATLien_0
      235
    4. 4
      snowy owl
      219
    5. 5
      Edouard
      171
  • Tell a friend

    Love Neowin? Tell a friend!