Dialog Boxes
Now that you've had the opportunity to use the InputBox command on occasion, have you ever looked at the screen display of an InputBox and wished you could change the size of the window, the location of the window, the location of the buttons inside the window, the size and number of the data entry fields, and so forth? Or just wished you could design an input window so that users could type in information and the macro could then use and play with what was typed in? There is a way to do this using dialog boxes--but be warned, the concepts are easy but the actual implementation can be extremely intimidating and aggravating.
(This is not always the case with certain other Visual Basic-based macro languages, as other macro editors often include the ability to change the size and contents of a window using mouse and menus, while the computer takes care of figuring out the programming commands needed to achieve the look you want. You may find it easier--if you have access to one of those other programs--to create a dialog box using that program, then copy it over into the PfW macro editor and make the few changes necessary to make the resulting commands compatible with OML.)
Here is the program code for a sample dialog box along the lines of what is used by the InputBox command. Note that this is completely non-functional as is, in order to better explain the various parts. Elements in angle brackets represent strings that can be included as a string or as plain text in quotation marks; square brackets designate optional elements.
Begin Dialog <name> [x, y,] dx, dy, [<caption>]
Text x, y, dx, dy, <text>
TextBox x, y, dx, dy, .<func1>
OkButton x, y, dx, dy
CancelButton x, y, dx, dy
End Dialog
Dim <name2> As <name>
[Dialog <name2>] [z = Dialog(<name2>)]
As you can see by comparing the first line to the second to last line, the <name> of the dialog is used later on to define the way the elements in the dialog box are referenced by the rest of the macro.
The <caption> is the title (if any) you want to appear in the blue bar at the top of the window; if this is left out, the default is "Softbridge Basic Language", as you've probably seen on message boxes.
X = across, y = down. X = 1 is 1/4 the average width of a letter; since the font used does not have uniform-width letters, x = 2 is roughly the width of the letter "i", and x = 6 is roughly the width of the letter "m". Y = 1 is 1/8 of the height of a normal capital letter; y = 12 will give enough room to display the entirety of any capital letter or a letter with a tail. Dx and dy are the total width and height of the box/window being defined. In the Begin Dialog statement, x and y are optional; they define where on the screen to place the box. If they are left out, the default is a central location.
<Name2> is defined to be the results of dialog <name>; because of this, <name2>, rather than <name>, is used by the rest of the macro to refer to the various elements of the dialog box. That is, <name2>.<func1> (for example, SampleDialog.Button1) is a variable containing whatever was typed in the textbox.
For the Dialog statement, one or the other form must be used, but not both at the same time, as here. The latter method works best if the only controls you have in the box are buttons; it equates to -1 if the Ok button is pressed, 0 if the Cancel button is pressed, and 1 and higher for any user-defined buttons, in the order of the Buttons commands in the macro. The former method seems to work best when there are a lot of boxes rather than buttons. However, it requires the use of an error handling routine if you want to continue or don't want an error box to pop up, as pressing the Cancel button causes in the macro to generate a "Command failed" error.
Here is a macro that creates and handles a sample dialog box:
sub main
StartDialog:
Begin Dialog GetInfo 130, 60, "Name Request"
Text 4, 4, 84, 12, "Please enter a name:"
TextBox 4, 20, 45, 12, .UserName
OkButton 4, 34, 40, 20
CancelButton 45, 34, 40, 20
PushButton 86, 34, 40, 20, "Reset"
OptionGroup .NameOrder
OptionButton 86, 4, 42, 12, "First Last"
OptionButton 86, 16, 42, 12, "Last, First"
End Dialog
Dim NameInfo As GetInfo
z = Dialog(NameInfo)
If z = -1 Then
Goto Done
ElseIf z = 1 Then
Goto StartDialog
Else
If NameInfo.UserName = "" Then
MsgBox "'Name' is a required field."
Goto StartDialog
End If
End If
If NameInfo.NameOrder = 0 Then
MsgBox "The name you typed was:" & NameInfo.NameOrder
Else
LaName$ = Trim(GetField(a$, 1, ","))
FirName$ = Trim(GetField, a$, 2, ","))
MsgBox "The name you typed was:" & FirName$ & " " & LaName$
End If
done:
end sub
As you can see, right away the total size of the window is established, as is the title of the window. Since there are only two numbers included, the window will be automatically centered on the screen. Then comes a Text command, which simply writes the indicated text in the window you've created (in this case, instructions for what to type in the box), and a TextBox command, in which the user can type some data. After that are three button commands: an Ok button, a Cancel button, and a custom button called "Reset". The last statements before the "End Dialog" are an OptionGroup that creates two radio buttons that will dictate how the macro handles the data the user enters.
The command "z = Dialog(NameInfo)" actually displays the window on the screen, and then waits for one of the three buttons to be clicked. Once that happens, an If...Then statement block routes the program flow appropriately, depending on which button was clicked. Assuming the user typed in data and clicked the Ok button, a second If...Then statement block manipulates the entered name--if necessary, as determined by the radio buttons--so that it is in the order "first-name last-name", and displays the name in a MsgBox.
Users of a dialog box which utilizes several of the above commands will be happy to note that it is possible to jump from any control to the next one by using the Tab key, and from any control to the previous one by using the Shift-Tab key combination. This can aid users who get confused when using a mouse or who often accidentally miss a control, as repeatedly pressing Tab or Shift-Tab will cycle through all of the controls, making each "active" in turn.
And that's that. The end...?