Dialog Boxes
Lesson 12 is quite long in comparison to previous lessons. You may wish to try it in smaller doses.
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.
Connexion comes with a dialog box editor, so you have the ability to create dialog boxes, add controls, and change the size and contents of a dialog box by using the mouse and menus, and the computer then takes care of figuring out the programming commands needed to achieve the look you want. You will usually find it easier to use that rather than manually create dialog boxes by hand, but you still need to know how the commands work in order to get data from the dialog box, and it can be helpful to know how to make some changes manually, so that you do not have to constantly open and close the dialog box editor. (It can be kind of like a refrigerator door in that regard.) Additionally, when you use the dialog box editor to make changes to existing dialog boxes, there may be some things it does not handle well, so it may accidentally wreck a dialog box that you've worked and worked on to get working just the way you want it to.
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 <name1> [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 <name1> 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.
Note that you generally must have at least one button showing on your dialog box, so that users have a way to exit from the dialog box. It does not have to be an OKButton or a CancelButton, but most dialog boxes will have one or the other or both of those two.
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 <name1>; because of this, <name2> is used by the rest of the macro to refer to the various elements of the dialog box rather than <name1>. That is, <name2>.<identifier> (for example, SampleDialog.MyTextBox) is a variable containing whatever was typed in the MyTextBox textbox of the SampleDialog dialog window.
The Dialog statement at the end is what actually displays the dialog box and waits for the user to do something; it closes the dialog box and the macro continues as soon as the user clicks any button. Z then reflects which button was pressed; -1 for an OKButton, 0 for a CancelButton, and 1 and higher for any user-defined buttons, in the order that the user-defined Button commands appear in the macro.
Since it may be easier to understand all of this by seeing these commands in their proper context, 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 LastName$ = Trim(GetField(a$, 1, ",")) FirstName$ = Trim(GetField, a$, 2, ",")) MsgBox "The name you typed was:" & FirstName$ & " " & LastName$ 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)" displays the dialog 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...?