{"id":45,"date":"2008-03-06T12:40:41","date_gmt":"2008-03-06T11:40:41","guid":{"rendered":"http:\/\/jimblackler.com\/blog\/?p=45"},"modified":"2008-03-06T12:40:41","modified_gmt":"2008-03-06T11:40:41","slug":"allowing-enter-or-return-in-controls-such-as-textbox-when-using-windows-forms-net-20","status":"publish","type":"post","link":"https:\/\/jimblackler.com\/?p=45","title":{"rendered":"Allowing Enter or Return in controls such as TextBox when using Windows Forms .NET 2.0"},"content":{"rendered":"<p><img src='http:\/\/jimblackler.com\/blog\/wp-content\/uploads\/2008\/03\/override2.gif' alt='A simple application overriding enter and return in a TextBox' title='A simple application overriding enter and return in a TextBox' align='right'\/><\/p>\n<p>Many applications feature text input boxes that used to accept input for immediate use by the program. For instance, a program might provide a dialog allowing user to search for text in a document. This would feature a text box to enter the text, and a button next to it labelled \u2018search\u2019 that would immediately act on the supplied text.<\/p>\n<p>However many users will not want to click the \u2018search\u2019 button, but simply press Return or Enter after entering the text.<\/p>\n<p>How can this be done in a C# .NET 2.0 Windows Forms application? One way is to set the Accept property in the parent form to refer to the \u2018search\u2019 button. This will make Enter \/ Return automatically press the button, when pressed in any child control on the form.<\/p>\n<p><img src='http:\/\/jimblackler.com\/blog\/wp-content\/uploads\/2008\/03\/override.gif' alt='The events to override in the designer' title='The events to override in the designer' align='right'\/><\/p>\n<p>This doesn\u2019t always offer the level of control the application developer wants. Any control could activate the press, not just the relevant text box. It requires a button, which might not always be desirable. Also it is not trivial to allow multiple textboxes on a single form that all accept Enter \/ Return in that way.<\/p>\n<p>The best way that I have found is to add a KeyDown event to the textbox and compare the pressed key with Keys.Enter. Then, act on that key and set SuppressKeyPress in the event arguments so that the system does not try to do anything further with the keypress. (Typically what happens otherwise is that an \u2018error\u2019 beep is played).<\/p>\n<div id=\"ig-sh-1\" class=\"syntax_hilite\">\n\n\t\n\t<div class=\"code\">\n\t\t<ol class=\"csharp\" style=\"font-family:monospace\"><li><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #0600FF;font-weight: bold\">private<\/span> <span style=\"color: #6666cc;font-weight: bold\">void<\/span> textBox1_KeyDown<span style=\"color: #008000\">&#040;<\/span><span style=\"color: #6666cc;font-weight: bold\">object<\/span> sender, KeyEventArgs e<span style=\"color: #008000\">&#041;<\/span><\/div><\/li>\n<li><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #008000\">&#123;<\/span><\/div><\/li>\n<li><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #0600FF;font-weight: bold\">if<\/span> <span style=\"color: #008000\">&#040;<\/span>e<span style=\"color: #008000\">.<\/span><span style=\"color: #0000FF\">KeyCode<\/span> <span style=\"color: #008000\">==<\/span> Keys<span style=\"color: #008000\">.<\/span><span style=\"color: #0000FF\">Enter<\/span> <span style=\"color: #008000\">&amp;&amp;<\/span> e<span style=\"color: #008000\">.<\/span><span style=\"color: #0000FF\">Modifiers<\/span> <span style=\"color: #008000\">==<\/span> Keys<span style=\"color: #008000\">.<\/span><span style=\"color: #0000FF\">None<\/span><span style=\"color: #008000\">&#041;<\/span><\/div><\/li>\n<li><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #008000\">&#123;<\/span><\/div><\/li>\n<li><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; &nbsp; &nbsp; <span style=\"color: #008080;font-style: italic\">\/\/ do something with textBox1.Text<\/span><\/div><\/li>\n<li><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; &nbsp; &nbsp; e<span style=\"color: #008000\">.<\/span><span style=\"color: #0000FF\">SuppressKeyPress<\/span> <span style=\"color: #008000\">=<\/span> <span style=\"color: #0600FF;font-weight: bold\">true<\/span><span style=\"color: #008000\">;<\/span><\/div><\/li>\n<li><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #008000\">&#125;<\/span><\/div><\/li>\n<li><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #008000\">&#125;<\/span><\/div><\/li>\n<\/ol>\t<\/div>\n\n<\/div>\n\n<p>However if you implement that code and run the application, you\u2019ll find nothing happens when you press Enter. The reason is that the Enter key press never reaches the KeyDown event. This happens because certain key presses are reserved for system use. The solution is to supply a PreviewKeyDown event in the textbox. This gives the opportunity to override the system override as it were, and allow the key to reach the KeyDown even in the textbox.<\/p>\n<div id=\"ig-sh-2\" class=\"syntax_hilite\">\n\n\t\n\t<div class=\"code\">\n\t\t<ol class=\"csharp\" style=\"font-family:monospace\"><li><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #0600FF;font-weight: bold\">private<\/span> <span style=\"color: #6666cc;font-weight: bold\">void<\/span> textBox1_PreviewKeyDown<span style=\"color: #008000\">&#040;<\/span><span style=\"color: #6666cc;font-weight: bold\">object<\/span> sender, PreviewKeyDownEventArgs e<span style=\"color: #008000\">&#041;<\/span><\/div><\/li>\n<li><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #008000\">&#123;<\/span><\/div><\/li>\n<li><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #0600FF;font-weight: bold\">if<\/span> <span style=\"color: #008000\">&#040;<\/span>e<span style=\"color: #008000\">.<\/span><span style=\"color: #0000FF\">KeyCode<\/span> <span style=\"color: #008000\">==<\/span> Keys<span style=\"color: #008000\">.<\/span><span style=\"color: #0000FF\">Enter<\/span> <span style=\"color: #008000\">&amp;&amp;<\/span> e<span style=\"color: #008000\">.<\/span><span style=\"color: #0000FF\">Modifiers<\/span> <span style=\"color: #008000\">==<\/span> Keys<span style=\"color: #008000\">.<\/span><span style=\"color: #0000FF\">None<\/span><span style=\"color: #008000\">&#041;<\/span><\/div><\/li>\n<li><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #008000\">&#123;<\/span><\/div><\/li>\n<li><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; &nbsp; &nbsp; e<span style=\"color: #008000\">.<\/span><span style=\"color: #0000FF\">IsInputKey<\/span> <span style=\"color: #008000\">=<\/span> <span style=\"color: #0600FF;font-weight: bold\">true<\/span><span style=\"color: #008000\">;<\/span><\/div><\/li>\n<li><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #008000\">&#125;<\/span><\/div><\/li>\n<li><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #008000\">&#125;<\/span><\/div><\/li>\n<\/ol>\t<\/div>\n\n<\/div>\n\n<p>I\u2019ve provided source for an example application as a zip file. Requires Visual Studio 2005 or later, C# express will also work.<\/p>\n<p><a href='http:\/\/jimblackler.com\/blog\/wp-content\/uploads\/2008\/03\/overrideenterandreturn.zip' title='Source code'>Download source code<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Many applications feature text input boxes that used to accept input for immediate use by the program. For instance, a program might provide a dialog allowing user to search for text in a document. This would feature a text box to enter the text, and a button next to it labelled \u2018search\u2019 that would immediately [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-45","post","type-post","status-publish","format-standard","hentry","category-windows-applications"],"_links":{"self":[{"href":"https:\/\/jimblackler.com\/index.php?rest_route=\/wp\/v2\/posts\/45","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jimblackler.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jimblackler.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jimblackler.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/jimblackler.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=45"}],"version-history":[{"count":0,"href":"https:\/\/jimblackler.com\/index.php?rest_route=\/wp\/v2\/posts\/45\/revisions"}],"wp:attachment":[{"href":"https:\/\/jimblackler.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=45"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jimblackler.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=45"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jimblackler.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=45"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}