• 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

    • "Can" is fine. I want the ability for it to "not." Actually, I would prefer vice versa. But it has to be optional, otherwise companies wouldn't be too happy.
    • ChatGPT can now connect to Outlook, Teams, Gmail, Google Drive, and other services by Pradeep Viswanathan Apart from regular consumers, ChatGPT is also growing fast among business users. In fact, OpenAI now has 3 million paying (Enterprise, Team, and Edu) business users, up from 2 million in February. During a live stream today targeted toward business users, OpenAI announced that ChatGPT can now connect to more external services to pull in real-time context and provide more useful responses for users. The following are some of the external connectors available in ChatGPT Deep Research for Plus, Pro, Team, Enterprise, and Edu users: Microsoft Outlook Microsoft Teams Microsoft SharePoint Dropbox Box Google Drive Gmail Liner In addition to the above, IT admins in organizations can now build custom ChatGPT connectors using the popular Model Context Protocol (MCP). These custom connectors will allow organizations to make use of the data available inside their proprietary systems and other apps within ChatGPT, alongside pre-built connectors. Today, the ChatGPT team also announced a new feature called record mode for ChatGPT Team users on macOS. The record mode feature will allow users to capture meeting audio and get meeting transcriptions, meeting action items, summaries, etc. ChatGPT record mode will also be available to Plus, Pro, Enterprise, and Edu users in the future. Kevin Weil, Chief Product Officer at OpenAI, tweeted the following regarding today’s launch: While ChatGPT's new connectors and features mark significant progress, it faces a formidable challenge in the enterprise market against Microsoft 365 Copilot, which enjoys native integration within the Microsoft 365 ecosystem.
    • I decided to give this a try and, wow. I like it. One of the most customizable browsers I've ever used I think, and it even has the classic menu bar. That's one of the reasons I've stuck with Firefox for so long. Not sure if I'll make this my main browser yet but it's looking good so far.
    • No messaging app from a large commercial entity is really encrypted to a point they are actually unable to access your content. If you trust whatsapp and friends to do this either you are a fool.
    • From my point of view this seems like a good thing? Option to pay as you go in an otherwise Subscription app? Yes Please! Free Windsurf + BYOK = Now I have a reason to try Windsurf.
  • Recent Achievements

    • Apprentice
      DarkShrunken went up a rank
      Apprentice
    • Dedicated
      CHUNWEI earned a badge
      Dedicated
    • Collaborator
      DarkShrunken earned a badge
      Collaborator
    • Rookie
      Pat-Garrett went up a rank
      Rookie
    • Week One Done
      Outdoor Saunaio earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      331
    2. 2
      snowy owl
      165
    3. 3
      +FloatingFatMan
      158
    4. 4
      ATLien_0
      154
    5. 5
      Xenon
      127
  • Tell a friend

    Love Neowin? Tell a friend!