There are two processes for adding controls to a form in ATEasy, at design time, and dynamically at run time. The topic of adding controls at design time and referrencing the control objects from an array are discussed in Knowledgebase article Q200161. This article is an extension of Q200161 and will focus on using arrays to access controls that were placed on the form dynamically at run time.
The InsertControl() method allows you to create a control during run-time. The syntax is:
[ ctrl = ] Object.InsertControl ( sControlName, sControlClassType, [fLeft], [fTop], [fWidth], [fHeight], [sContainer], [bVisible], [vNameOrIndex] )
A full description of this method, and the associated AddHandler statement can be found in the ATEasy help document. An example of inserting a new GroupBox with eight Switches and a Label onto an existing ATEasy form is shown below. This code duplicates controls that were placed on the form at design time. Note: Code for setting the dynamically created controls properties and creating an event handlers, while not discussed, were included for completeness. The resulting form and controls are shown in figure 1:
Module Variables:
objGB: AGroupBox
objLBL: ALabel
aobjGroup: ASwitch[8]
Procedure:
lWidth: Long
lHeight: Long
lPosX: Long
lPosY: Long
i: Long
lWidth=gb1.Width
lHeight=gb1.Height
lPosx=gb1.Left
lPosY=gb1.Top+lHeight+50
i=Form.ControlsCount
! Add new GroupBox control to form "MyForm" and set the Caption property
objGB=Form.InsertControl("objGB","AGroupBox",lPosX,lPosY,lWidth,lHeight,"MyForm",True,i)
objGB.Caption="Run-Time Controls"
! Add new Label control to form "MyForm" and set properties
objLBL=Form.InsertControl("objLBL","ALabel",170,lPosY+lHeight+10,76,17,"MyForm",True,i+1)
objLBL.Alignment=alblCenter
objLBL.Font.Name="MS Sans Serif"
objLBL.Font.Size=8
objLBL.Font.Bold=True
objLBL.Caption="Byte Value"
! Add eight Switch control to the new GroupBox control and set properties
For i=0 to 7
aobjGroup[i]=Form.InsertControl("swGB"+Str(i),"ASwitch",343-i*47,41,24,58,"objGB",True,i)
aobjGroup[i].Caption=""
aobjGroup[i].OffText=""
aobjGroup[i].OnText=""
aobjGroup[i].Style=4
! Add an OnSwitchChange event handler for each new switch created
AddHandler aobjGroup[i], "OnChange", OnSwitchChange
Next
OnSwitchChange Procedure(objCtrl):
objCtrl: VAL AControl
lByteValue: Long
i: Long
For i=0 to 8
lByteValue=lByteValue+((2^i)*aobjGroup[i].Value)
Next
objLBL.Caption=Str(lByteValue)
Figure 1 - Design Time and Run Time generated controls
Refer to the Forms.PRG example in the ATEasy Examples.