How Can I Position the Cursor in a Specific Text Box Each Time I Start an HTA?
Hey, Scripting Guy! How can I position the cursor in a specific text box each time I start an HTA?
Hey, BM. You know, if the Scripting Guy who writes this column has any regrets in life it’s that he didn’t 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’s way cooler than concocting a script that can position the cursor in a specific text box each time you start an HTA.
OK, sure, if you want to get picky about it these theories really only explain about 4% of the universe. But that’s the other cool thing about theoretical physics. Is 96% of the universe missing? No problem: obviously that 96% must 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’ll never be able to prove even exist. Case closed!
Note. 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’s impossible to get rid of any of them: even the so-called “universal remotes” 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.
Of course, as a Scripting Guy you can’t 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:
<html> <head> <title>HTA Test</title> <HTA:APPLICATION ID="objTest" APPLICATIONNAME="HTATest" SCROLL="yes" SINGLEINSTANCE="yes" > </head> <SCRIPT LANGUAGE="VBScript"> Sub Window_OnLoad BasicTextbox.Focus End Sub </SCRIPT> <body> <input type="text" name="BasicTextbox" size="30"> </body>
As you can see, there’s 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 BasicTextbox. Here’s the HTML tagging that creates that text box:
<input type="text" name="BasicTextbox" size="30">
There’s nothing special here, either, except that we gave the text box a name. That’s 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.
If you’re even interested in doing that sort of thing, that is.
Oh, that’s right: you are interested in doing that sort of thing, aren’t you? Well, fortunately, our HTA also includes a subroutine named Window_Onload that positions the cursor for you:
Sub Window_OnLoad BasicTextbox.Focus End Sub
Again, this is almost-embarrassingly simple. All you really have to do are two things. First, make sure you give the subroutine the name Window_Onload. 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.
Second, the notion of “positioning the cursor” 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 Focus method. Hence the one line of code we truly care about:
When you start or refresh this HTA the cursor will automatically be positioned in the text box. At that point, happy typing!
Hope that answers your question, BM. Incidentally, if you happen to be a theoretical physicist we have a tip for you: if you’re 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 are at least three basketballs, a football, several baseballs, and a Wiffle ball bat). However, if dark energy and dark matter really do exist that’s as good a place as any to start looking.