{"id":63573,"date":"2007-11-16T00:41:00","date_gmt":"2007-11-16T00:41:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2007\/11\/16\/hey-scripting-guy-how-can-i-create-a-self-updating-text-box-in-an-hta\/"},"modified":"2007-11-16T00:41:00","modified_gmt":"2007-11-16T00:41:00","slug":"hey-scripting-guy-how-can-i-create-a-self-updating-text-box-in-an-hta","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-can-i-create-a-self-updating-text-box-in-an-hta\/","title":{"rendered":"Hey, Scripting Guy! How Can I Create a Self-Updating Text Box in an HTA?"},"content":{"rendered":"<p><H2><IMG 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\"> <\/H2>\n<P>Hey, Scripting Guy! I have an HTA with three text boxes. In box 1 you enter a first name; in box 2 you enter a last name. I\u2019d like box 3 to automatically update itself based on what you type in the other text boxes. For example, if you type <I>Ken<\/I> in box 1 then box 3 shows <I>Ken<\/I>. If you then type <I>Myer<\/I> in box 2 box 3 shows <I>Ken Myer<\/I>. How do I do that?<BR><BR>&#8212; MH<\/P><IMG border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><IMG 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 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> \n<P>Hey, MH. You know, the one question we Scripting Guys get asked the most is this: Can you tell us a little bit about the early history of Barcelona? And, sadly, for the longest time our answer was always the same: sorry, but no, we can\u2019t tell you much of <I>anything<\/I> about the early history of Barcelona. That has bothered us for years, and so \u2013 after careful consideration \u2013 we decided to blow most of our budget, travel to Spain (ostensibly for <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/topics\/teched07\/default.mspx\"><B>TechEd IT Forum<\/B><\/A>) and find out a little bit about the history of Barcelona.<\/P>\n<P>What\u2019s that? Couldn\u2019t we have just read about that in a book or looked it up on the Internet? Hmmm \u2026. You know, we never thought of that. Wonder if it\u2019s too late to get our money back?<\/P>\n<P>At any rate, for those of you who have been wondering, Barcelona was founded by the Romans in the third century BC. (Some say it was founded by Hercules himself.) However, the city didn\u2019t truly gain prominence until towards the end of the 9<SUP>th<\/SUP> century AD, when Wilfred the Hairy seized control over several neighboring lands and established the kingdom of Catalonia.<\/P>\n<TABLE id=\"EXD\" 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>. Why the name \u201cWilfred the Hairy\u201d? Well, according to one legend, this was because Wilfred had hair in places that most people <I>don\u2019t<\/I> have hair. Which places? The legend doesn\u2019t say and, to tell you the truth, we didn\u2019t ask.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Anyway, after Wilfred the Hairy was killed in battle hardly anything of interest happened in Barcelona. Well, at least not until the Scripting Guys showed up and wrote a script that could auto-update a text box in an HTML Application (HTA):<\/P><PRE class=\"codeSample\">&lt;html&gt;\n&lt;head&gt;\n    &lt;title&gt;Self-Updating Text Box&lt;\/title&gt;\n&lt;\/head&gt;<\/p>\n<p>&lt;SCRIPT Language=&#8221;VBScript&#8221;&gt;\n    Sub SetFullName\n        CombinedName.Value = FirstName.Value &amp; &#8221; &#8221; &amp; LastName.Value\n     End Sub\n&lt;\/SCRIPT&gt;<\/p>\n<p>&lt;body&gt;\n    &lt;input type=&#8221;text&#8221; name=&#8221;FirstName&#8221; size=&#8221;25&#8243; onChange=&#8221;SetFullName&#8221;&gt;\n    &lt;input type=&#8221;text&#8221; name=&#8221;LastName&#8221; size=&#8221;25&#8243; onChange=&#8221;SetFullName&#8221;&gt;\n    &lt;input type=&#8221;text&#8221; name=&#8221;CombinedName&#8221; size=&#8221;51&#8243; readOnly=True&gt;\n&lt;\/body&gt;\n<\/PRE>\n<P>Let\u2019s see if we can figure out exactly how this thing works. As is the case with most HTAs, we basically have two sections here: the &lt;BODY&gt; section (the place where we put all the controls), and the &lt;SCRIPT&gt; section (which, as you might expect, is the place where we put all our scripts). For demonstration purposes, our &lt;BODY&gt; section is remarkably compact; it consists entirely of three text boxes, one named FirstName, one named LastName, and one named CombinedName (sounds like some sort of scripting fairy tale, doesn\u2019t it):<\/P><PRE class=\"codeSample\">&lt;input type=&#8221;text&#8221; name=&#8221;FirstName&#8221; size=&#8221;25&#8243; onChange=&#8221;SetFullName&#8221;&gt;\n&lt;input type=&#8221;text&#8221; name=&#8221;LastName&#8221; size=&#8221;25&#8243; onChange=&#8221;SetFullName&#8221;&gt;\n&lt;input type=&#8221;text&#8221; name=&#8221;CombinedName&#8221; size=&#8221;51&#8243; readOnly=True&gt;\n<\/PRE>\n<P>As you can see, the HTML tags for the first two text boxes (FirstName and LastName) are very similar. We have the <B>&lt;input&gt;<\/B> tag, which includes the <B>type=&#8221;text&#8221;<\/B> parameter, the parameter that distinguishes the text box from other input controls (like a button, checkbox, or radio button). We have the <B>name<\/B> parameter (followed by the unique name given to this text box), and the <B>size<\/B> parameter (which simply sets the width of the box to, in this case, 25 characters).<\/P>\n<P>We also have one other parameter to deal with: <B>onChange=&#8221;SetFullName&#8221;<\/B>. This is the parameter that enables our auto-updating text boxes to function properly. Any time the value in either of the first two text boxes is changed, the onChange event is fired. (Technically the event is fired after the change has been made <I>and<\/I> the text box has lost the focus.) What\u2019s going to happen any time the onChange event is fired, at least for the first two text boxes in our HTA? You got it: we\u2019re going to run the subroutine SetFullName.<\/P>\n<P>You might have noticed that our third text box \u2013 CombinedName \u2013 doesn\u2019t have an onChange event. Why? Well, for one reason, no one is ever going to type anything in this text box; this is the box that gets automatically updated based on what we type in the other two boxes. In fact, to make <I>sure<\/I> that no one types anything into this text box we add the parameter <B>readOnly=True<\/B>. Try to guess whether or not this turns the text box into a read-only text box.<\/P>\n<P>Well, you\u2019re right: it does. <\/P>\n<P>Lucky guess.<\/P>\n<P>So much for the &lt;BODY&gt; section. As for the &lt;SCRIPT&gt; section, this features a single subroutine (SetFullName), a subroutine which, in turn, features a single line of code:<\/P><PRE class=\"codeSample\">CombinedName.Value = Trim(FirstName.Value &amp; &#8221; &#8221; &amp; LastName.Value)\n<\/PRE>\n<P>All we\u2019re doing here is assigning something to the <B>Value<\/B> property of our third text box (CombinedName). And what exactly are we assigning to the Value property? We\u2019re assigning the Value of the first text box (FirstName) plus a blank space plus the value of the second text box (LastName). Oh, and then we\u2019re using the <B>Trim<\/B> function to remove any excess blank spaces at the beginning or the end of this value.<\/P>\n<P>What does that give us? Well, suppose you type <I>Ken<\/I> in the first text box. When that box loses the focus (e.g., when you press tab or click outside the box), the onChange event fires and text box 3 will be set to this:<\/P><PRE class=\"codeSample\">Ken\n<\/PRE>\n<P>Why <I>Ken<\/I>? Because that\u2019s the value of box 1 plus a blank space plus the value of text box 2 (which is currently nothing). But then shouldn\u2019t this be <I>Ken_<\/I>, with the underscore representing a blank space? Well it <I>would<\/I> be, except that we used the Trim function to trim off any leading or trailing blank spaces.<\/P>\n<P>Now suppose we type <I>Myer<\/I> in box 2. Try to guess what box 3 will equal <I>now<\/I>.<\/P>\n<P>Wow; you guys are either <I>really<\/I> lucky guessers, or you\u2019ve already caught on to how this works. Either way, yes, box 3 will now be equal to this:<\/P><PRE class=\"codeSample\">Ken Myer\n<\/PRE>\n<P>If we go back and change box 1 to <I>Kenneth<\/I> then box 3 will automatically reset itself to this:<\/P><PRE class=\"codeSample\">Kenneth Myer\n<\/PRE>\n<P>Etc., etc., etc.<\/P>\n<P>Incidentally, Wilfred the Hairy was the first Catalonia leader to pass his crown down to his son. This hereditary passing down of titles is an interesting one, to say the least. For example, does that mean that the Scripting Guy who writes this column will eventually pass that title \u2013 and this column \u2013 down to the Scripting Son?<\/P>\n<P>No, probably not. After all, the Scripting Guy who writes this column likes <I>his<\/I> son.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! I have an HTA with three text boxes. In box 1 you enter a first name; in box 2 you enter a last name. I\u2019d like box 3 to automatically update itself based on what you type in the other text boxes. For example, if you type Ken in box 1 then [&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-63573","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! I have an HTA with three text boxes. In box 1 you enter a first name; in box 2 you enter a last name. I\u2019d like box 3 to automatically update itself based on what you type in the other text boxes. For example, if you type Ken in box 1 then [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/63573","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=63573"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/63573\/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=63573"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=63573"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=63573"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}