Forms and Scripts

Creating a form automatically creates an empty script that is associated with the form. The script refers to controls in the form to achieve the programmatic functioning desired for the form.

 

By default, the script uses VBSCript language. Suppose we create a new form in the project. The minimum script created by default simply shows the form:

 

Sub Main

 Form.Visible = True

End Sub

 

We can write a more elaborate script that upon the press of a button displays a message box showing "Hello, World!" To do this, we would first add a command button to the form.

 

images\btn_ctrl_button.gif Using the Insert Command Button tool insert a command button into the form as described in the Editing Forms topic. Right click onto the new command button, choose Properties and in the Control tab change the default Name to Hello and in the General tab change the default Caption to Hello.

 

images\eg_forms_scripts_01.gif

 

The form with the new command button is seen above in a form window open for editing. We can now click open the form's script and add code to handle a click of the Hello button. This will be an additional Sub:

 

Sub Main

 Form.Visible = True

End Sub

 

' -- Launch message box on click

Sub Hello_Click

 MsgBox "Hello, World!"

End Sub

 

If we run the form from the project pane it creates a form:

 

images\eg_forms_scripts_02.gif

 

Every time we click the Hello button a message box will launch with our desired text:

 

images\eg_forms_scripts_03.gif

 

We can click OK on the message box and then click Hello to display the message again as often as we like. To close the form we can click on the X box in the upper right corner. We can add a second button, a Close button that will close the form using our own command button.

 

images\btn_ctrl_button.gif To do so we use the Insert Command Button tool again to insert a second command button into the form. Right click onto the new command button, choose Properties and in the Control tab change the default Name to Close and in the General tab change the default Caption to Close.

 

After a little tinkering with the alignment commands as described in the Editing Forms topic we have two buttons that are the same size and neatly aligned.

 

In the form's script we add a Sub to handle clicks of the Close button:

 

Sub Main

 Form.Visible = True

End Sub

 

' -- Launch message box on click

Sub Hello_Click

 MsgBox "Hello, World!"

End Sub

 

' -- close form

Sub Close_Click

 Form.Visible = False

End Sub

 

images\eg_forms_scripts_04.gif

 

Running the form now shows it has two command buttons. The Hello button pops open a message box and the Close button closes the form.

 

A more sophisticated form would use controls like text boxes to display text that is generated programmatically. For example, we might create a form to view the contents of the Employees table of the Nwind.mdb sample database:

 

images\eg_forms_scripts_05.gif

 

The script that powers this form is directly analogous to the simple example with two buttons. The script launches with a Main routine that makes the form visible. A Close button closes the form. Routines for each button apply some program logic and call the Reload routine to redisplay the form. The Reload routine loads up the various text boxes with appropriate text strings taken from the table record.

 

' -- launch form on startup

Sub Main

 Form.Visible = True

End Sub

 

' ---------------------------

 

Dim Employees

EmployeeIndex = -1

EmployeeCount = 10

 

' ---------------------------

 

' -- jump to first employee on load

Sub Form_OnLoad

 

 ' -- set up global variables

 Set Components = Application.ActiveDocument.ComponentSet

 EmpPos = Components.ItemByName("Employees")

 If EmpPos < 0 Then

  MsgBox "'Employees' table not found."

  Form.Visible = False

 End If

 Set Employees = Components(EmpPos)

 EmployeeIndex = 0

 EmployeeCount = Employees.RecordSet.Count

 

 ' -- load controls

 Reload

End Sub

 

' -- close form

Sub Close_Click

 Form.Visible = False

End Sub

 

' -- jump to first employee

Sub First_Click

 If EmployeeIndex <> 0 Then

  EmployeeIndex = 0

  Reload

 End If

End Sub

 

' -- jump to last employee

Sub Last_Click

 If EmployeeIndex <> EmployeeCount-1 Then

  EmployeeIndex = EmployeeCount-1

  Reload

 End If

End Sub

 

' -- jump to next employee if any

Sub Next_Click

 If EmployeeIndex < EmployeeCount-1 Then

  EmployeeIndex = EmployeeIndex+1

  Reload

 End If

End Sub

 

' -- jump to previous employee if any

Sub Previous_Click

 If EmployeeIndex > 0 Then

  EmployeeIndex = EmployeeIndex-1

  Reload

 End If

End Sub

 

' -- load current employee

Sub Reload

 Set Rec = Employees.RecordSet(EmployeeIndex)

 

 ' -- set employee readouts

 Employee.Text = Rec.Data("First Name") _

& " " & Rec.Data("Last Name") & " (#" & CStr(EmployeeIndex+1) & ")"

 Title.Text = Rec.Data("Title")

 BirthDate.Text = Rec.Data("Birth Date")

 HireDate.Text = Rec.Data("Hire Date")

 Country.Text = Rec.Data("Country")

 Region.Text = Rec.Data("Region")

 City.Text = Rec.Data("City")

 PostalCode.Text = Rec.Data("Postal Code")

 Address.Text = Rec.Data("Address")

 Notes.Text = Rec.Data("Notes")

End Sub

 

We've named the text box controls the same as the text captions (which are separate controls), so that code like…

 

Region.Text = Rec.Data("Region")

 

… refers to the text box named Region.

 

The form shown above allows us to step through the Employees table using First, Previous, Next and Last buttons. With each press of a button the appropriate record info will be displayed in the text boxes. The form is intended as a simple example but even so it is quite useful.

 

We could extend the form above by adding one more button, a Select button to select the record currently shown in the table. Just like adding buttons in the two button example at the beginning of this topic, we use the Insert Command Button tool to draw a new command button in the form. Right click onto the new command button, choose Properties and in the Control tab change the default Name to Select and in the General tab change the default Caption to Select. Use the alignment tools to make the button the same size as the other buttons and aligned neatly in the same row. We've placed the new Select button in the lower left corner of the form.

 

images\eg_forms_scripts_06.gif

 

We can then add one more Sub to the script to handle any clicks of the Select button:

 

Sub Select_Click

 ' clear any selection

 Set records = Employees.RecordSet

 For nItem = 0 To records.Count-1

  Set record = records( nItem )

  record.Mask = record.Mask And Not 1

 Next

 ' create selection

  Set record = Employees.RecordSet(EmployeeIndex)

  record.Mask = record.Mask Or 1

End Sub

 

This routine first clears the selection and then selects whatever is the current employee seen in the form.

 

Using Languages other than VBScript

 

Form scripts can use languages other than VBScript, for example, Javascript. The syntax of event handlers will vary from language to language.

 

Here is the Javascript version of the script for the form discussed at the beginning of this topic:

 

function Main() {

Form.Visible = true;

}

 

function Hello::Click() {

Application.MessageBox("Hello, World!"); }

 

Note that form scripts cannot use .NET languages such as C# or VB .NET. This is only a minor issue since .NET scripts can create forms in code, without using form components, through the use of Windows Forms. Advanced script writers will also frequently design forms outside of Manifold using Visual Studio .NET or other tools, and call these forms from Manifold scripts.