インスタンスマネージャーを使用した実行時のサブフォームの制御

この例は、インスタンスマネージャーのプロパティとメソッドを使用して、実行時にサブフォームに関する情報を取得し、サブフォームオブジェクトで操作を行う方法を示しています。

この例では、フォームの入力者がボタンと Subform3 のインスタンスを使用して様々なアクションを実行します。例えば、フォームの入力者が「次のインスタンスを追加」ボタンをクリックすると、新しい Subform3 のインスタンスが現在のインスタンスの下に追加されます。

注意: サブフォームを追加または削除するか、テキストフィールド内のデータを変更して、Subform3 のインスタンスに適用されたその変更を確認する必要がある場合があります。
注意: 特定のサブフォームのインスタンスがフォームに存在しない場合は、次のそれぞれの例に示すように、アンダースコア(_)を使用する必要があります。アンダースコア(_)の使用について詳しくは、Designer のヘルプを参照してください。

「新規サブフォームを追加」ボタンのスクリプティング

次のスクリプトでは、サポートされている最大数の Subform3 のインスタンスがフォームに存在するかどうかを判断します。最大数のインスタンスが存在する場合は、スクリプトによってメッセージが表示されます。それ以外の場合は、新しい Subform3 のインスタンスがフォームに追加されます。

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

次に示すように、アンダースコア(_)を使用してインスタンスマネージャーのプロパティとメソッドを参照することにより、このスクリプトを記述することもできます。

    if (advanced._Subform3.count == advanced._Subform3.max) { 
        xfa.host.messageBox("You have reached the maximum number of items 
        allowed.", "Combining Instance Manager Concepts", 1); 
    } 
    else { 
        advanced._Subform3.addInstance(1); 
        xfa.form.recalculate(1); 
    }

「サブフォームを削除」ボタンのスクリプティング

次のスクリプトでは、Subform3 のインスタンスがフォームに存在するかどうかを判断します。インスタンスが存在しない場合は、そのことを示すメッセージがスクリプトによって表示されます。インスタンスが存在する場合は、スクリプトによって最初のインスタンスがフォームから削除されます。

    if (advanced.Subform3.instanceManager.count == 0) { 
        xfa.host.messageBox("There are no subform instances to remove.",  
        "Combining Instance Manager Concepts", 1);     
    } 
    else { 
        advanced.Subform3.instanceManager.removeInstance(0); 
    }

次に示すように、アンダースコア(_)を使用してインスタンスマネージャーのプロパティとメソッドを参照することにより、このスクリプトを記述することもできます。

    if (advanced._Subform3.count == 0) { 
        xfa.host.messageBox("There are no subform instances to remove.", 
        "Combining Instance Manager Concepts", 1);     
    } 
    else { 
        advanced._Subform3.removeInstance(0); 
    }

「次のインスタンスを追加」ボタンのスクリプティング

次の if-else ステートメントでは、最大数の Subform3 のインスタンスがフォームに含まれている場合、スクリプトを続行しないようにします。

    if (Subform3.instanceManager.count < Subform3.instanceManager.occur.max) { 
//oNewInstance stores an instance of Subform3 created by the addInstance() method. 
    var oNewInstance = Subform3.instanceManager.addInstance(1); 
//nIndexFrom and nIndexTo store the before and after index values to use with the moveInstance() method.  
    var nIndexFrom = oNewInstance.index; 
    var nIndexTo = Subform3.index + 1;

この場合、スクリプトが nIndexFrom の値を参照すると、moveInstance メソッドで指定されたフォーム上の位置に新しい Subform3 のインスタンスが追加されます。

        Subform3.instanceManager.moveInstance(nIndexFrom, nIndexTo); 
    } 
    else { 
        xfa.host.messageBox("You have reached the maximum number of items 
        allowed.", "Combining Instance Manager Concepts", 1); 
    }

次に示すように、アンダースコア(_)を使用してインスタンスマネージャーのプロパティとメソッドを参照することにより、このスクリプトを記述することもできます。

    if (_Subform3.count < _Subform3.occur.max) { 
    var oNewInstance = _Subform3.addInstance(1); 
    var nIndexFrom = oNewInstance.index; 
    var nIndexTo = Subform3.index + 1; 
    _Subform3.moveInstance(nIndexFrom, nIndexTo); 
    } 
    else { 
    xfa.host.messageBox("You have reached the maximum number of items allowed.",         "Combining Instance Manager Concepts", 1); 
    }

「このインスタンスを削除」ボタンのスクリプティング

次の if-else ステートメントでは、最小数の Subform3 のインスタンスがフォームに含まれている場合、スクリプトを続行しないようにします。

    if (Subform3.instanceManager.count > Subform3.instanceManager.occur.min) {

次のスクリプトでは、removeInstance メソッドを使用して Subform3 のインスタンスを削除します。

注意: このスクリプトでは、値 parent.parent.index を使用して、削除する Subform3 のインスタンスを示します。parent 参照は、この参照を使用するオブジェクトのコンテナを示します。この場合、参照 parent.index を使用して、「次のインスタンスを追加」、「このインスタンスを削除」、「1 行上に」および「1 行下に」の各ボタンを含む名称未設定のサブフォームを示します。
        Subform3.instanceManager.removeInstance(parent.parent.index); 
    } 
    else { 
        xfa.host.messageBox("You have reached the minimum number of items 
        allowed.", "Combining Instance Manager Concepts", 1); 
    }

次に示すように、アンダースコア(_)を使用してインスタンスマネージャーのプロパティとメソッドを参照することにより、このスクリプトを記述することもできます。

    if (_Subform3.count > _Subform3.occur.min) { 
    Subform3.removeInstance(Subform3.index); 
    } 
    else { 
    xfa.host.messageBox("You have reached the minimum number of items allowed.",  
    "Combining Instance Manager Concepts", 1); 
    }

「1 行上に」ボタンのスクリプティング

次の if-else ステートメントでは、Subform3 のインスタンスが最初のインスタンスとしてリストに表示されている場合、スクリプトを続行しないようにします。

    if (Subform3.index != 0) { 
//nIndexFrom and nIndexTo store the before and after index values to use with the moveInstance method.  
        var nIndexFrom = Subform3.index; 
        var nIndexTo = Subform3.index - 1; 
        Subform3.instanceManager.moveInstance(nIndexFrom, nIndexTo); 
    } 
    else { 
        xfa.host.messageBox("The current item cannot be moved because it is the  
        first instance in the list.", "Combining Instance Manager Concepts", 1); 
    }

次に示すように、アンダースコア(_)を使用してインスタンスマネージャーのプロパティとメソッドを参照することにより、このスクリプトを記述することもできます。

    if (Subform3.index != 0) { 
        var nIndexFrom = Subform3.index; 
    var nIndexTo = Subform3.index - 1; 
    Subform3.moveInstance(nIndexFrom, nIndexTo); 
    } 
    else { 
    xfa.host.messageBox("The current item can't be moved since it already is the     first instance in the list.", "Combining Instance Manager Concepts", 1); 
    }

「1 行下に」ボタンのスクリプティング

次の変数は、Subform3 のインスタンスのインデックス値を保存します。

    var nIndex = Subform3.index;

次の if-else ステートメントでは、Subform3 のインスタンスが最後のインスタンスとしてリストに表示されている場合、スクリプトを続行しないようにします。

    if ((nIndex + 1) < Subform3.instanceManager.count) { 
    // nIndexFrom and nIndexTo store the before and after index values to use with the moveInstance() method.  
        var nIndexFrom = nIndex; 
        var nIndexTo = nIndex + 1; 
     
        Subform3.instanceManager.moveInstance(nIndexFrom, nIndexTo); 
        } 
        else { 
        xfa.host.messageBox("The current item cannot be moved because it is the last 
        instance in the list.", "Combining Instance Manager Concepts", 1); 
        }

次に示すように、アンダースコア(_)を使用してインスタンスマネージャーのプロパティとメソッドを参照することにより、このスクリプトを記述することもできます。

    var nIndex = Subform3.index; 
    if ((nIndex + 1) < Subform3.instanceManager.count) { 
    var nIndexFrom = nIndex; 
    var nIndexTo = nIndex + 1; 
    _Subform3.moveInstance(nIndexFrom, nIndexTo); 
    } 
    else { 
    xfa.host.messageBox("The current item can't be moved since it already is the 
    last instance in the list.", "Combining Instance Manager Concepts", 1); 
    }