&<img src="../resources/images/opentocr.png" title="Open Contents list" border="0"&>
You are here:

Example 5: Dynamically Create Buttons

There are many programming situations where an action requires the creation and population of a collection, passing the collection to another component for processing and finally destroying the collection.
This example shows how to dynamically create a keyed collection of buttons, work with them, and how to then destroy all the buttons from memory:

A Dynamic Collection
This example first creates a dynamic collection of buttons:
 
DEFINE_COM class(#PRIM_KCOL<#prim_phbn #STD_NUM>) name(#BUTTON_COLLECTION) reference(*DYNAMIC)  STYLE(Collection)
 
By specifying reference(*DYNAMIC) you are telling LANSA not to create the collection when the form is loaded (as happens with an ordinary DEFINE_COM statement), but to create it only when a reference is explicitly made to it. The benefit of explicitly creating the collection is that later on you will be able to destroy it at any given time by setting the reference to it to *NULL.
Collection and Buttons Are Created at Run-Time

At run-time, when the user enters the number of buttons to be created and clicks the Create Buttons button and if the collection does not already exist, it is created using the SET_REF command:
 
IF_REF com(#BUTTON_COLLECTION) is(*null)
SET_REF com(#BUTTON_COLLECTION) to(*Create_as #PRIM_KCOL<#prim_phbn #STD_NUM>)
ENDIF
And then the number of buttons specified in the #STD_NUM field are created in the collection:
 
BEGIN_LOOP to(#STD_NUM)
CHANGE field(#BUTTINDEX) to('#ButtIndex + 1')
USE builtin(BCONCAT) with_args('Push Button' #BUTTCHAR) to_get(#STD_TEXTL)
SET_REF com(#Button_collection<#ButtIndex>) to(*CREATE_AS #PRIM_PHBN)
SET com(#BUTTON_COLLECTION<#ButtIndex>) PARENT(#Button_Panel) WIDTH(120) CAPTION(#Std_TextL)
END_LOOP
Lastly they are realized: