Programming Guide : Creating Dynamic Frames : How You Can Create and Modify Fields Dynamically : How You Can Create Dynamic Simple Fields
 
Share this page          
How You Can Create Dynamic Simple Fields
Dynamically creating simple fields (ShapeFields or ScalarFields) is a simple four-step process:
1. Declare a reference pointer to a field of the appropriate type.
Unless you declare it with default null, the declaration creates an instance of the object. The following code declares and instantiates a free trim object called nextquarter:
initialize ()=
declare
    nextquarter = FreeTrim;
    labelstart = integer not null;
    linechart_subform = Subform;
    i = integer not null;
    name_array = array of StringObject;
    bfield = ButtonField;
    stack_field = StackField;
    efields = EntryField;
    colname = varchar(11) not null;
    dexp = DynExpr default null;
enddeclare
2. Create a new instance of the field if necessary.
The following code creates a new free trim object called nextquarter:
nextquarter = FreeTrim.Create();
3. Set field attributes.
The following code sets attributes for position and point size:
nextquarter.AbsXleft = labelstart;
nextquarter.TypeSize = 10;
4. Attach the field to the form by giving it a parent. Fields are attached to a form in the following examples:
The following code adds a dynamically created field directly to the form:
nextquarter.ParentField = CurFrame.TopForm;
The following code adds a dynamically created field to a composite field called linechart_subform:
linechart_subform = Subform.Create();
nextquarter.ParentField =
    field(linechart_subform);
If the parent field is a MatrixField or Stackfield, the field is placed into a specific cell of the parent with the InsertChild method.
The following code creates multiple buttons, getting their label text from an array of button names, and inserts the buttons into a stack field:
for i = 1 to name_array.LastRow do
    bfield = ButtonField.Create();
    field(bfield).TextLabel = name_array[i];
    stack_field.InsertChild(fieldtoinsert =
        bfield, position = i);
endfor;
After the field has been attached to the form, you can reuse the same reference variable to create another field. If you reuse the reference variable, however, you no longer have a direct way to point to your original field and, therefore, have no simple way to manipulate the field's attributes or data or remove it from the form.
If you want to keep reference variables to all fields you create, use an array. For example, the following code declares an array of EntryField objects and creates each new entry field as a member of this array:
efields = Array of EntryField;
...
j = j + 1;
efields[j] = EntryField.Create();
The ChildFields attribute of the CompositeField system class stores a reference variable pointing to each field contained in the composite field. The ChildFields attribute often provides the best way to look at all the fields in a composite, although stepping through all the child fields of CurFrame.TopForm looking for a particular field can be awkward.
Because the ChildFields attribute makes it easy to reference the fields in a StackField object, the example for Step 4 uses a single ButtonField reference variable instead of an array.
If you are creating several fields of the same type that share most of their attributes, you can duplicate the first and then change attribute values as appropriate, for example:
efields[j+1] = efields[j].Duplicate();
If you create an entry field dynamically, its style defaults to multi-line. To allow the user to tab from entry field to entry field, change the dynamically created entry field to single-line by setting the IsMultiLine attribute to FALSE. The following code makes this change:
efields[].IsMultiLine = FALSE;