August 19, 2004

Trapping Enter key in Windows Forms TextBox

Suppose you want to do something special when the user hits enter in one of your Windows Forms app's TextBoxes? "Easy!", you say, thinking about hooking up a KeyDown event handler - until you try it and find out it doesn't actually work. And then you go Googling, just like I did earlier today.

And yeah, it's true, you can't catch keys reserved for form navigation with KeyDown or KeyPress events. Well, that blows. Of course, a quick search of the web turns up quite a few workarounds, so you're saved. Since I figured this out already, let me save you some time.

When a key is hit in a TextBox control, a method named IsInputKey gets called on the control. That method takes a Keys enumeration as a parameter and returns a bool - true if that particular key is an "input key", false if not. False also means that the form handler will take care of (or ignore) the keystrike instead of passing it over to the control. And you guessed it, TextBox.IsInputKey returns false for Keys.Enter (this is different if you have a multiline textbox and AcceptReturn enabled, but let's not go there now).

So, your problem is solved once you make your TextBoxes accept enter as an input key. The answer is subclassing the textbox, i.e. creating your own control. That's not really as bad as it sounds. In fact, you can get away with just this:

public class EnterTextBox : TextBox {
  protected override bool IsInputKey(Keys key) {
    if (key == Keys.Enter) 
      return true;
    return base.IsInputKey(key);
  }
}

Now start using your new textbox controls. If you're using no IDE at all or you have Visual Studio, just go and substitute new EnterTextBox() for new System.Windows.Forms.TextBox(). VS's Form Designer seems to handle this quite nicely. If you're using #develop, you can't use that shortcut - #develop forms designer will wipe away your textboxes. Luckily your EnterTextBox will have appeared in the Custom Components section of the toolbox, so it's only a matter of dragging some controls.

As you can guess, the following event handler now works properly:

void TextBox1_KeyDown(object sender, KeyEventArgs e) {
  if (e.KeyCode == Keys.Enter) {
    e.Handled = true;
    MessageBox.Show("Enter hit in textbox1!");
  }
}

Remember to set Handled to true to signal the textbox that the keystroke has actually been dealt with already.

If you did the Googling part, you probably saw there are a few other solutions to this as well. One of them involves overriding ProcessDialogKey, and another common one is overriding the WndProc handler. They both get notifications for enter hits regardless of what IsInputKeys returns. If you have a static definition for what enter should mean (for example, always clear the field), you could use one of those approaches. However, if the action required varies by field, you should use the solution above to override IsInputKey instead - that way you can hook into the KeyDown event to customize behavior on an instance-by-instance basis.

Posted by Jouni Heikniemi at August 19, 2004 09:29 PM
.net

Comments

Hi,

first, thanks for the article, helped me a lot.

When I first tried to use your solution, I ran into some problems - althought I made everything as you did, with the difference that I was not using a TextBox but a ToolStripTextBox.

I modified your code to override the ToolStripTextBox - but did have no success the overridden Method was never called.

When I replaced the ToolStripTextBox with a regular TextBox everything worked fine.

Posted by: Lex Alexander at August 5, 2005 11:44 AM

Thanks!

Posted by: Richard at December 19, 2005 11:39 AM

I have this implemented for the tab & shift-tab key. Everything works as I want, but every time the tab or shift-tab key is pressed a beep sound is generated! Does anyone know why ? and how to prevent it from happening.
Not sure if this matters but my inherited textbox control resides on a usercontrol (and not a dialog or form)
Thanks.

Posted by: Cliff at January 25, 2006 09:25 PM

You can also try this:

private void txtMyTextBox_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
//if the user presses the Enter key, do some action
int ascii = (int)e.KeyChar;

if (ascii == 13)
{
e.Handled = true;
this.Something();
}
}

Posted by: Brian at February 28, 2006 09:05 PM

I like your solution Brian.
Saved me a lot of work with this trick.

Thx :)

Posted by: Pieter at March 14, 2006 03:50 PM

gierki erotyczne cum

Posted by: olo at March 30, 2006 03:13 PM

http://www.filmy-porno-banged.polskiexxx.com

Posted by: olo at March 30, 2006 03:14 PM

Thank you. This was very helpful.

I did note one thing that may help others.

I originally encountered this problem on a form with the AcceptButton property set to an "OK" button.

When I reset the AcceptButton property to "(none)", I was able to intercept the ENTER button without overriding the IsInputKey method.

For me this, was an easier change. I simply communicate the DialogResult differently.

-Eric.

Posted by: Eric at May 26, 2006 11:40 PM

you sir are a legend. congratulations upon receiving such an accolade.

saved me a good spot of google trawling (tm)

cheers buddy

;)

Posted by: brief thanks at August 21, 2006 01:12 PM

Hmm - just tried this in .NET 2.0, and it seems that KeyPress is fired when an Enter is sent to a vanilla textbox. Is this something that's likely to have been fixed in the new version?

Posted by: xtian at December 18, 2006 05:06 PM

Oops - changed the form so that the AcceptButton was set, and saw exactly the behaviour you describe. Thanks!

Posted by: xtian at December 18, 2006 05:19 PM

e.Handled = True. Too right.

Thanks a lot! IsInputKey's been driving me nuts.

Posted by: Yuri at March 26, 2007 09:57 AM

Nice, just that I needed.

Alllan
/Denmark

Posted by: Allan Kørup at March 28, 2007 09:37 AM

XQNByx khnfigwccqrw, [url=http://iouvepfblcym.com/]iouvepfblcym[/url], [link=http://uhvpsxnpmekf.com/]uhvpsxnpmekf[/link], http://mykoqeqfhbvt.com/

Posted by: iatlza at March 28, 2008 12:46 AM

Spring's releases: , porn gangbang free pics, [url="http://www.blackplanet.com/Derek137/"]porn gangbang free pics[/url], http://www.blackplanet.com/Derek137/ porn gangbang free pics, uuejxu, celebrity porn pics, [url="http://us.cyworld.com/AngelaK5"]celebrity porn pics[/url], http://us.cyworld.com/AngelaK5 celebrity porn pics, cuqs, ebony porn reviews, [url="http://www.blackplanet.com/Jaadin/"]ebony porn reviews[/url], http://www.blackplanet.com/Jaadin/ ebony porn reviews, 29025, gay japanese porn, [url="http://us.cyworld.com/Densil"]gay japanese porn[/url], http://us.cyworld.com/Densil gay japanese porn, 8OOO,

Posted by: Aram11l at April 10, 2008 04:29 PM

Thanks a lot. Great solution :-)

Posted by: Dameure at April 10, 2008 10:22 PM

Freedom, brotherhood! , fee porn for women, [url="http://www.blackplanet.com/der3r4x/"]fee porn for women[/url], http://www.blackplanet.com/der3r4x/ fee porn for women, free gay sex porn videos, [url="http://www.blackplanet.com/o3hh4v/"]free gay sex porn videos[/url], http://www.blackplanet.com/o3hh4v/ free gay sex porn videos, free streaming porn clips, [url="http://us.cyworld.com/s34bnn"]free streaming porn clips[/url], http://us.cyworld.com/s34bnn free streaming porn clips, young softcore, [url="http://www.blackplanet.com/so3f4t/"]young softcore[/url], http://www.blackplanet.com/so3f4t/ young softcore,

Posted by: Glena019 at April 20, 2008 12:18 AM

Don't panic! , lindsay lohan nude pictures, [url="http://www.imeem.com/people/gQlVUBf/blogs/2008/04/25/2VsMKn_t/lindsay_lohan_nude"]lindsay lohan nude pictures[/url], http://www.imeem.com/people/gQlVUBf/blogs/2008/04/25/2VsMKn_t/lindsay_lohan_nude lindsay lohan nude pictures,

Posted by: Todd456 at April 26, 2008 05:52 PM

Alles egal bei uns gibt es das beste

Posted by: teens at July 12, 2008 05:42 PM
Post a comment









Remember personal info?