How do I test to see if the Enter key has been pressed within a TextBox? I want to initiate an action if either the corresponding button is pressed (I can do that) or if Enter is pressed after the user puts info into the TextBox.
C# 2008 handling Enter key in a text boxI want to initiate an action when the Enter key is pressed
Page 1 of 1
6 Replies - 25026 Views - Last Post: 14 August 2009 - 03:50 PM
Replies To: C# 2008 handling Enter key in a text box
#2
Re: C# 2008 handling Enter key in a text box
Posted 14 August 2009 - 12:19 PM
This is pretty simple bit of code for a _KeyDown Function. I basically got tired of when i was testing my code that involved such things having to actually move my mouse and all that. I hope it helps. The names of methods have been changed to protect their identity.
private void Method_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
Method(sender, e);
}
}
This post has been edited by CruorAvis: 14 August 2009 - 12:38 PM
#3
Re: C# 2008 handling Enter key in a text box
Posted 14 August 2009 - 12:21 PM
Handle the KeyDown event of the Textbox, within KeyEventArgs, use KeyCode to determine if it's enter or not.
EDIT: Damn I was beaten
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Return)
{
// Code to run on enter/return
}
}
EDIT: Damn I was beaten
This post has been edited by MageUK: 14 August 2009 - 12:22 PM
#4
Re: C# 2008 handling Enter key in a text box
Posted 14 August 2009 - 12:23 PM
I'm not sure if this is the most effective method, but I would tackle it this way:
EDIT: Damn I was double beaten
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
// Do something
}
}
EDIT: Damn I was double beaten
This post has been edited by prankster: 14 August 2009 - 12:24 PM
#5
Re: C# 2008 handling Enter key in a text box
Posted 14 August 2009 - 12:35 PM
Try something like this.. you basically want to assign a keypress event handler and do a check inside the event handler to see if it was the Enter key that was pressed..
The value for the Enter Key is 13 by the way.
If you need help with how to assign an event handler,in visual studio 2005 it's pretty easy. Select the text box in the design mode and look at the properties box. There is a lightning bolt near the top of that box when clicked will show you all of the events for that control. double click to the right of the event you want.. (the keypress preview in this case, or you could use the keydown event just as easily)
clicking it should bring you to the code view with a function inserted for the event. This is your new event handler.
Hope this helped!
Aet
EDIT:: triple beaten.. with better code even.. LOL Dag!
what are you guys hovering on the boards waiting for posts?
The value for the Enter Key is 13 by the way.
private void textBox1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
if (e.KeyValue.ToString() == "13")
MessageBox.Show("Enter Key Pressed!");
}
If you need help with how to assign an event handler,in visual studio 2005 it's pretty easy. Select the text box in the design mode and look at the properties box. There is a lightning bolt near the top of that box when clicked will show you all of the events for that control. double click to the right of the event you want.. (the keypress preview in this case, or you could use the keydown event just as easily)
clicking it should bring you to the code view with a function inserted for the event. This is your new event handler.
Hope this helped!
Aet
EDIT:: triple beaten.. with better code even.. LOL Dag!
what are you guys hovering on the boards waiting for posts?
This post has been edited by Aeternalis: 14 August 2009 - 12:37 PM
#7
Re: C# 2008 handling Enter key in a text box
Posted 14 August 2009 - 12:36 PM
Looks like I've been beaten to the punch!
Though my code will work to invoke a button or such it doesn't actually check to see if it was done from say a Textbox. This is what you need to check from a TextBox.
(Added into the Form1.Designer.cs)
Then in your code enter the following method:
Though my code will work to invoke a button or such it doesn't actually check to see if it was done from say a Textbox. This is what you need to check from a TextBox.
(Added into the Form1.Designer.cs)
this.textBox1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(CheckKeys);
Then in your code enter the following method:
private void CheckKeys (object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if (e.KeyChar == (char)13)
{
// Then Enter key was pressed
// Put whatever you want it to do here
}
This post has been edited by CruorAvis: 14 August 2009 - 12:38 PM
#8
Re: C# 2008 handling Enter key in a text box
Posted 14 August 2009 - 03:50 PM
I used many of the above suggestions but was mainly helped by realizing that other events are available from the Properties window. I just clicked on the appropriate TextBox then scrolled down in the Events to KeyPressed and defined my own handler (txtFileLoc_TextChanged) then double clicked on the TextBox and got the handler skeleton. The code was just the 2 lines within the handler below.
private void txtFileLoc_KeyPressed(object sender, KeyPressEventArgs e)
{
if ( e.KeyChar == 13 )
GetHtmlPage();
}
Works great.
Thank you.
private void txtFileLoc_KeyPressed(object sender, KeyPressEventArgs e)
{
if ( e.KeyChar == 13 )
GetHtmlPage();
}
Works great.
Thank you.
Page 1 of 1

New Topic/Question
Reply



MultiQuote



|