Programming Guide : 15. Writing a Template Assistant : How You Can Write an Assistant : Example--Field Template Assistant
 
Share this page                  
Example--Field Template Assistant
In this example, a field template assistant procedure has been designed to customize a field template.
The field template consists of a composite field that contains several entry fields and one text field for the title. The field template assistant procedure prompts the user for the title and assigns that text string to the text field of the composite field.
First, the parameters are defined:
Procedure Create_Label
(
ff = FormField not null,
return_code = integer not null,
batch_mode = integer not null
)=
Because the procedure must support batch mode, a default string is specified. Otherwise, the user is prompted for the string:
if batch_mode = TRUE then
text_string.value = 'Hello, world!';
else
status  = curprocedure.replypopup(messagetext =
'Enter text string.',
reply = text_string);
if status != PU_OK then
return_code = ER_FAIL;
return;
endif;
endif;
Next, the text field with the variable name "label" is loaded with the specified text string:
field_obj = CompositeField(ff).FieldByName(name =
        'label');
EntryField(field_obj).textvalue = text_string.value;
Finally, the procedure needs to assign a value to the return code parameter:
return_code = ER_OK;
How You Can Attach the Assistant to a Field Template
In the same application that contains the frame assistant procedure Create_Label, a field template can be created (called, for example, My_Field_Template). This template should contain one composite field that consists of several entry fields, and one free trim field for the label with the variable name of “label.” To associate the field template My_Field_Template with its assistant, open the Property Inspector for the field template in its editor, and specify Create_Label as the assistant procedure.
When a form field is subsequently created from the template My_Field_Template, the procedure Create_Label is called, which populates the new label with the text string entered by the user.
The full text of the field assistant procedure is as follows:
Procedure Create_Label
(
ff = FormField not null,
return_code = integer not null,
batch_mode = integer not null
) =
declare
text_string = StringObject;
status = integer not NULL;
enddeclare
begin
{
if batch_mode = TRUE then
text_string.value = 'Hello, world!';
else
status  = curprocedure.replypopup(messagetext
    = 'Enter text string.',
        reply = text_string);
if status != PU_OK then
    return_code = ER_FAIL;
    return;
endif;
endif;
field_obj = CompositeField(ff).FieldByName(name
    =  'label');
EntryField(field_obj).textvalue =
    text_string.value;
return_code = ER_OK;
return;
}
end;