InputBoxes
As described in the previous lesson, variables can be assigned a value by the programmer. Another way to assign a value to a variable is by the user. There are several ways to do this. The easiest will be discussed here.
Input Boxes are a simple way to get data from the user. The InputBox$ command displays a dialog box containing a prompt. The program waits for the user to enter text, and the user either accepts or cancels the dialog box. Then, the program returns the contents of the text box and assigns the content to a variable.
The general syntax is:
<variableName> = InputBox$(Prompt, Title, DefaultText)
Here is an expanded example (adapted from a script by Mark_D):
Dim s As String Value = ""
Dim Prompt As String Value = "Enter some text."
Dim Title As String Value = "InputBox Example"
Dim DefaultText As String Value = "Type something"
Dim sMsg As String Value = ""
'---Assign the variable 's' to what is entered in the INPUTBOX
s = InputBox$(Prompt ,Title, DefaultText )
'---Prepare the 'sMsg' variable
sMsg = " You entered: " & $CRLF
sMsg += s & $CRLF
'---Display a Message Box to show what the user entered in the INPUTBOX
MsgBox 0, sMsg
$CRLF is an equate or constant. It is used in the assigning of variables to bring the text to the next line. It is like hitting the Enter button when typing in a word processor. The letters stand for Carriage Return Line Feed.
There are other ways to get information from the user. These will be discussed in further tutorials.
Output with MsgBox & Alert
In the previous example, a simple message box was used to display the variable assigned by the user through the input box. The syntax of the message box allows for a few more options:
<variableName> = MsgBox(hParent, Message, Style, Title, Timeout, UpdateCountDown, CountDownFormat)
"hParent" is a refrence to the parent window. If "0" is used, there is no parent window assigned to the message box. "Message" is a string containing the message to be displayed in the box. As in the simple example above, the string can be passed in a variable. The remaining characteristics of the message box are optional: Style, Title, Timeout, UpdateCountDown, and CountDownFormat.
A long list of "Style" equates or constants can be used to alter the look of the message box. The message box can have a title other than "thinBasic" if the "Title" parameter is used. An optional Timeout period can be specified: after the specified period, if no reply from user, a specific timeout message will be returned allowing script to make default or automatic decisions. The last two parameters are further options for the timeout parameter. Here's an example using all of the parameters:
Dim s As Long
s = MsgBox(0, "This is a message.",%MB_ICONEXCLAMATION,"Custom Message Box - ",10000, 100,"0.0")
This example displays a message box for 10 seconds, updating the countdown every 1/10 of a second, displaying the countdown with one digit after the decimal.
The Alert is meant to send a message to the user with no extra options. The box displays close to the system tray at the bottom of the user's screen. Its syntax is as follows:
<variableName> = Alert(Title, Message, Timeout)
|