Text and Password Fields in VBScript

This page will describe how to use text fields and password fields in your ScriptDialog in VBScript.

Create a new VBScript script. Then insert the following code and save it as tut5.vbs.

 Listing 5.1, tut5.vbs 
Sub main
  ' Create the dialog box
  Set Dialog = CreateObject("TPFSoftware.ScriptDialog")


  ' Add a text field  Dialog.AddTextField "ID", "User:", 15, "frank"
  ' Add a password field  Dialog.AddPasswordField "Pswd", "Password:", 15
  ' Add a row of buttons  Dialog.AddButtons "button", Array("OK", "Cancel")
  ' Display the dialog box  Set Result = Dialog.Execute
  ' Find out which button was clicked  If Result.ValueOf("button") = "OK" Then     ID = Result.ValueOf( "ID" )     Pswd = Result.ValueOf( "Pswd" )     MsgBox "The password for " & ID & " is " & Pswd   Else     MsgBox "You did not press OK"   End If End Sub

When you run the script, you'll see a dialog box that looks like Figure 5 below. Clicking a button will display a message telling you what you did.

Figure 5: Dialog box produced by tut5.vbs.

Analysis
To add an editable field to a dialog box, use the AddTextField method. AddTextField takes five arguments, the last two optional. The first is the name of the text field you are adding. The second is the label that will appear beside the text field. The third is the visual width of the text field in characters. The fourth (and an optional argument) is the default text value for the field. And the fifth (and optional) argument is the maximum length in characters that the user is allowed to type into the text field.

For example, the statement...

Dialog.AddTextField "ID", "User:", 15, "frank", 12

...adds a text field named "ID" to the dialog box. The label to the left of the text field is "User:". The text field appears 15 characters wide, and the default value of the field is the text "frank." The final argument specifies the maximum number of characters the user is allowed to type into the field—in this case, 12.

To add a password field to a dialog box, use the AddPasswordField method. AddPasswordField takes the same number and types of arguments as AddTextField.

To find out what the user typed into a text field or password field, use the ValueOf method which belongs to the Result object. ValueOf takes the name of the field and returns its value.