AddObject Method

Adds an object to a Container object at run time.


Object.AddObject(cName, cClass [, cOLEClass] [, aInit1, aInit2 ...])

cName

Specifies the name used to reference the new object.

cClass

Specifies the class of the object to add.

cOLEClass

Specifies the OLE class of the object to add.

Note:

If you plan to distribute run-time applications which add ActiveX Controls requiring License key information (for example, Microsoft Treeview and Listview controls), you should not use the AddObject(.....cOLEClass) method. Instead, create and save a subclass of your ActiveX Control to a VCX class library. Then, at run time you can use the AddObject (or NewObject) method to add this OleControl class containing your control.

aInit1, aInit2

Specify parameters passed to the Init event of the new object.

Applies To: Column Object | CommandGroup Control | Container Object | Custom Object | DataEnvironment Object | Form Object | FormSet Object | Grid Control | OptionGroup Control | Page Object | PageFrame Control | _SCREEN | ToolBar Object

Calling the AddObject method triggers the Init event of the object being added. When a form is added to a form set, the Load event occurs before the Init event.

Note:

When you use AddObject to add an object to a container, the object's Visible property is set to False (.F.) so you can set the properties of the object without any awkward visual effects as the object's appearance changes.

If you use the AddObject( ) method to add an ActiveX Control to a form dynamically, your distributed application can fail if that control requires certain licensing registry keys that are not available on the target machine. Many vendors of ActiveX controls require such registry keys for manipulation of these controls at design time as with the AddObject( ) method. Avoid this licensing requirement by first subclassing the ActiveX Control in a class library file (.vcx) and then using AddObject( ) to add an instance of the subclass dynamically at run time. You should always check with the vendor of any ActiveX control you plan to distribute with your custom application since there may be other dependent files that you need to include.

The following example demonstrates how the AddObject method can be used to add objects or controls to a form. AddObject is used to add a Line control and three command buttons to the form.

The Visible property is set to True (.T.) for the Line control and the command buttons. By default, objects and controls are not visible when they are added to a form.

frmMyForm = CREATEOBJECT('Form')  && Create a Form
frmMyForm.Closable = .F.  && Disable the Control menu box 

frmMyForm.AddObject('shpLine','Line')  && Add a Line control to the form
frmMyForm.AddObject('cmdCmndBtn1','cmdMyCmndBtn1')  && Up Cmnd button
frmMyForm.AddObject('cmdCmndBtn2','cmdMyCmndBtn2')  && Down Cmnd button
frmMyForm.AddObject('cmdCmndBtn3','cmdMyCmndBtn3')  && Quit Cmnd button

frmMyForm.shpLine.Visible = .T.  && Make Line control visible
frmMyForm.shpLine.Top = 20  && Specify Line control row
frmMyForm.shpLine.Left = 125  && Specify Line control column

frmMyForm.cmdCmndBtn1.Visible =.T.  && Up Command button visible
frmMyForm.cmdCmndBtn2.Visible =.T.  && Down" Command button visible
frmMyForm.cmdCmndBtn3.Visible =.T.  && Quit Command button visible

frmMyForm.SHOW  && Display the form
READ EVENTS  && Start event processing

DEFINE CLASS cmdMyCmndBtn1 AS COMMANDBUTTON  && Create Command button
   Caption = 'Slant \<Up'  && Caption on the Command button
   Left = 50  && Command button column
   Top = 100  && Command button row
   Height = 25  && Command button height
   
   PROCEDURE Click
      ThisForm.shpLine.Visible = .F.  && Hide the Line control
      ThisForm.shpLine.LineSlant ='/'  && Slant up
      ThisForm.shpLine.Visible = .T.  && Show the Line control
ENDDEFINE

DEFINE CLASS cmdMyCmndBtn2 AS CommandButton  && Create Command button
   Caption = 'Slant \<Down'  && Caption on the Command button
   Left = 200  && Command button column
   Top = 100  && Command button row
   Height = 25  && Command button height

   PROCEDURE Click
      ThisForm.shpLine.Visible = .F.  && Hide the Line control
      ThisForm.shpLine.LineSlant ='\'  && Slant down
      ThisForm.shpLine.Visible = .T.  && Show the Line control
ENDDEFINE

DEFINE CLASS cmdMyCmndBtn3 AS CommandButton  && Create Command button
   Caption = '\<Quit'  && Caption on the Command button
   Cancel = .T.  && Default Cancel Command button (Esc)
   Left = 125  && Command button column
   Top = 150  && Command button row
   Height = 25  && Command button height

   PROCEDURE Click
      CLEAR EVENTS  && Stop event processing, close Form
ENDDEFINE

Community Additions

Show: