Using the methods of the instance manager to control subforms

This example demonstrates how to use the methods of the instance manager (which is part of the XML Form Object Model) to perform operations on subform objects at run time. For example, you can add remove instances of a particular subform, table, or table row.

In the following form, the form filler uses the four buttons to use the various instance manager scripting methods. For example, when the form filler clicks the Add button a new Subform2 instance is added to the form.

Note: The Move button reorders the first two Subform2 instances, and the Set button displays the maximum number of Subform2 instances. In both cases, you may need to add or remove subforms, or make changes to the data in the text fields to see the changes applied to the Subform2 instances.

Scripting to determine whether you added the maximum number of subforms to a form

The following script determines whether the supported maximum number of Subform2 instances exist on the form. If the maximum number exists, the script displays a message. Otherwise, a new Subform2 instance is added to the form.

    if (methods.Subform2.instanceManager.count == 
    methods.Subform2.instanceManager.max) { 
    xfa.host.messageBox("You have reached the maximum number of items allowed.", 
    "Instance Manager Methods", 1); 
} 
    else { 
    methods.Subform2.instanceManager.addInstance(1); 
    xfa.form.recalculate(1); 
}

You can also write this script by using the underscore (_) notation to reference the properties and methods of the instance manager, as shown here:

    if (methods._Subform2.count == methods._Subform1.max) { 
         xfa.host.messageBox("You have reached the maximum number of items  
        allowed.", "Instance Manager Methods", 1); 
} 
    else { 
        methods._Subform2.addInstance(1); 
        xfa.form.recalculate(1); 
}

Scripting to determine whether there are more subforms to remove on the form

The following script determines whether any Subform2 instances exist on the form. If none exist, the script displays a message indicating that no instances exist. If instances do exist, the script removes the first instance from the form.

    if (methods.Subform2.instanceManager.count == 0) { 
        xfa.host.messageBox("There are no subform instances to remove.", 
        "Instance Manager Methods", 1);     
    } 
    else { 
        methods.Subform2.instanceManager.removeInstance(0); 
        xfa.form.recalculate(1); 
    }

You can also write this script by using the underscore (_) notation to reference the properties and methods of the instance manager, as shown here:

    if (methods._Subform2.count == 0) { 
        xfa.host.messageBox("There are no subform instances to remove.", 
        "Instance Manager Methods", 1);     
    } 
    else { 
        methods._Subform2.removeInstance(0); 
        xfa.form.recalculate(1); 
    }

Scripting to force four subform instances to appear on the form

The following script forces four Subform2 instances to appear on the form regardless of how many instances currently exist:

    methods.Subform2.instanceManager.setInstances(4);

You can also write this script by using the underscore (_) notation to reference the properties and methods of the instance manager, as shown here:

    methods._Subform2.setInstances(4);

Scripting to force the first and second subforms to switch locations on the form

The following script forces the first and second Subform2 instances to switch locations on the form.

    methods.Subform2.instanceManager.moveInstance(0,1);

You can also write this script by using the underscore (_) notation to reference the properties and methods of the instance manager, as shown here.

    methods._Subform2.moveInstance(0,1);

// Ethnio survey code removed