HTA
Tutorial #3In this tutorial, I'll show how the computer can appear to be a bit more intelligent, able to make decisions based on input and modify it's output. This is achieved using the IF statement. I won't be repeating the entire text file as in previous tutorials so you will need to read them first. Here then is a simple IF script:-
Option Explicit
Dim MyVar
Dim MyInput
MyInput = InputBox("Enter
a name", "Name")
IF MyInput = "nick" then
MsgBox ("Hello " & MyInput)
end if
As before, two variables are created (or DIMensioned)
ready for use. We use the InputBox as before to get input from
the user and store it in MyInput. This is then used by the IF
statement to decide wether or not to display the input. As it
stands, this is not terribly useful as only the exact user input
(in this case the word nick in lower case) will
allow the message box to be displayed. It would be more useful if
the program could point out to the user the kind of input
required if it is wrong. This is where the else
statement makes an appearance.
Dim MyVar
dim MyInput
MyInput = InputBox("Enter a name", "Name")
if MyInput = "nick" then
MsgBox ("Hello " & MyInput)
else
MsgBox ("Incorrect name " & MyInput & ".
Should be nick")
end if
Now a message will be displayed no matter what is input. Note that an error condition will still occur even if the word Nick or NIck (or any other upper/lower case mixture) is input. To get around this, use the command LCase. Replace the following line:-
MyInput = InputBox("Enter
a name", "Name")
with
MyInput = LCase(InputBox("Enter
a name", "Name"))
This will take the output from the InputBox function (in this
case a name) and convert it to lower case. Note that the variable
MyVar is changed and is thus displayed in lower case in the
message box.
So now it is possible to show you another MsgBox function which will prove useful in the future. It looks like this.
Option Explicit
Dim MyVar
MyVar = MsgBox ("Hello World!", 65, "MsgBox
Example")
MsgBox MyVar
When run, the message box will display the return value of the MsgBox function which in this case will either be 1 or 2 depending on the button pressed by the user. This can be used as follows.
Option Explicit
Dim MyVar
MyVar = MsgBox ("Select either button", 65, "MsgBox
Example")
IF MyVar = 1 then
MsgBox "OK Returns " & MyVar
else
MsgBox "Cancel Returns " & MyVar
End If
So now, it will be possible to ask the user wether to carry out some operation or not (like overwriting an existing file for example) without resorting to text input but instead using only a single mouse click. Have a play with these programs and see if you can add your own variations. The next tutorial will be a little more taxing than this one as we'll be investigating loops.