{"id":65973,"date":"2006-11-27T14:18:00","date_gmt":"2006-11-27T14:18:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/11\/27\/how-can-i-position-the-cursor-in-a-specific-text-box-each-time-i-start-an-hta\/"},"modified":"2006-11-27T14:18:00","modified_gmt":"2006-11-27T14:18:00","slug":"how-can-i-position-the-cursor-in-a-specific-text-box-each-time-i-start-an-hta","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-position-the-cursor-in-a-specific-text-box-each-time-i-start-an-hta\/","title":{"rendered":"How Can I Position the Cursor in a Specific Text Box Each Time I Start an HTA?"},"content":{"rendered":"<p><img decoding=\"async\" class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"><\/p>\n<p>Hey, Scripting Guy! How can I position the cursor in a specific text box each time I start an HTA?<\/p>\n<p>&#8212; BM<\/p>\n<p><img decoding=\"async\" border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><img decoding=\"async\" class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\"><a href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><img decoding=\"async\" class=\"farGraphic\" title=\"Script Center\" border=\"0\" alt=\"Script Center\" align=\"right\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" height=\"288\"><\/a><\/p>\n<p>Hey, BM. You know, if the Scripting Guy who writes this column has any regrets in life it\u2019s that he didn\u2019t become a Theoretical Physicist Guy. Why? Well, among other things, theoretical physicists have concocted theories that completely explain the nature and makeup of the universe. You have to admit, that\u2019s <i>way<\/i> cooler than concocting a script that can position the cursor in a specific text box each time you start an HTA.<\/p>\n<p>OK, sure, if you want to get picky about it these theories really only explain about 4% of the universe. But that\u2019s the other cool thing about theoretical physics. Is 96% of the universe missing? No problem: obviously that 96% <i>mus<\/i>t be composed of dark matter and dark energy, substances that, by definition, are likely undetectable. In other words, 96% of the universe is made up of substances we\u2019ll never be able to prove even exist. Case closed!<\/p>\n<table id=\"EED\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<thead><\/thead>\n<tbody>\n<tr class=\"record\" vAlign=\"top\">\n<td>\n<p class=\"lastInCell\"><b>Note<\/b>. Personally, the Scripting Guy who writes this column is skeptical of dark energy and dark matter. He believes that the missing 96% is, instead, made up entirely of remote controls; at the Scripting Guy household, for example, and in the living room alone, are remote controls for the TV, the cable box, the DVD player, the VCR, and the stereo. Best of all, it\u2019s impossible to get rid of any of them: even the so-called \u201cuniversal remotes\u201d seem to handle only 4% of all possible situations. Multiply the number of remote controls in a room by the number of rooms in a house and the number of houses in the universe and, well you get the idea.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"dataTableBottomMargin\"><\/div>\n<p>Of course, as a Scripting Guy you can\u2019t get away with saying that positioning the cursor in a specific text box in an HTA can only be done by using dark VBScript, code that cannot be typed on a keyboard or actually added to a script. Instead, Scripting Guys have to provide solutions that can be tested and implemented. You know, like this one:<\/p>\n<pre class=\"codeSample\">&lt;html&gt;\n&lt;head&gt;\n&lt;title&gt;HTA Test&lt;\/title&gt;\n&lt;HTA:APPLICATION\n     ID=\"objTest\"\n     APPLICATIONNAME=\"HTATest\"\n     SCROLL=\"yes\"\n     SINGLEINSTANCE=\"yes\"\n&gt;\n&lt;\/head&gt;\n&lt;SCRIPT LANGUAGE=\"VBScript\"&gt;\n    Sub Window_OnLoad\n        BasicTextbox.Focus\n    End Sub\n&lt;\/SCRIPT&gt;\n&lt;body&gt;\n    &lt;input type=\"text\" name=\"BasicTextbox\" size=\"30\"&gt;\n&lt;\/body&gt;\n<\/pre>\n<p>As you can see, there\u2019s really not much to this. In fact, all we have here is a very simple HTA, one sporting a single item: a text box named <i>BasicTextbox<\/i>. Here\u2019s the HTML tagging that creates that text box:<\/p>\n<pre class=\"codeSample\">&lt;input type=\"text\" name=\"BasicTextbox\" size=\"30\"&gt;\n<\/pre>\n<p>There\u2019s nothing special here, either, except that we gave the text box a name. That\u2019s the key: as long as the text box has a name you can refer to it in a script. And if you can refer to that particular text box then you can do all sorts of cool things, like automatically position the cursor in the box each time the HTA loads up. <\/p>\n<p>If you\u2019re even interested in doing that sort of thing, that is.<\/p>\n<p>Oh, that\u2019s right: you <i>are<\/i> interested in doing that sort of thing, aren\u2019t you? Well, fortunately, our HTA also includes a subroutine named <i>Window_Onload<\/i> that positions the cursor for you:<\/p>\n<pre class=\"codeSample\">Sub Window_OnLoad\n    BasicTextbox.Focus\nEnd Sub\n<\/pre>\n<p>Again, this is almost-embarrassingly simple. All you really have to do are two things. First, make sure you give the subroutine the name <i>Window_Onload<\/i>. In an HTA (or in a Web page for that matter) any subroutine named Window_Onload automatically runs each time the HTA is started or refreshed. Window_Onload: the autoexec.bat of the HTA world.<\/p>\n<p>Second, the notion of \u201cpositioning the cursor\u201d simply means giving the control in question (in this case, the text box) the focus. That can be done just by referencing the control by name and then calling the <b>Focus<\/b> method. Hence the one line of code we truly care about:<\/p>\n<pre class=\"codeSample\">BasicTextbox.Focus\n<\/pre>\n<p>When you start or refresh this HTA the cursor will automatically be positioned in the text box. At that point, happy typing!<\/p>\n<p>Hope that answers your question, BM. Incidentally, if you happen to be a theoretical physicist we have a tip for you: if you\u2019re still missing 96% of the universe you might check the hall closet in the home of the Scripting Guy who writes this column. To be honest, this Scripting Guy has absolutely no idea what all has been stuffed into that closet (although there <i>are<\/i> at least three basketballs, a football, several baseballs, and a Wiffle ball bat). However, if dark energy and dark matter really do exist that\u2019s as good a place as any to start looking.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I position the cursor in a specific text box each time I start an HTA? &#8212; BM Hey, BM. You know, if the Scripting Guy who writes this column has any regrets in life it\u2019s that he didn\u2019t become a Theoretical Physicist Guy. Why? Well, among other things, theoretical physicists [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[3,4,5,30],"class_list":["post-65973","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-scripting-guy","tag-scripting-techniques","tag-vbscript","tag-web-pages-and-htas"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I position the cursor in a specific text box each time I start an HTA? &#8212; BM Hey, BM. You know, if the Scripting Guy who writes this column has any regrets in life it\u2019s that he didn\u2019t become a Theoretical Physicist Guy. Why? Well, among other things, theoretical physicists [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/65973","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=65973"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/65973\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=65973"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=65973"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=65973"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}