Logo

Hacker’s Guide to Visual FoxPro
An irreverent look at how Visual FoxPro really works. Tells you the inside scoop on every command, function, property, event and method of Visual FoxPro.

AddObject, NewObject, RemoveObject

These methods let you add and remove objects from containers, both at design-time and at runtime. You can add controls to a form or page or column, columns to a grid, pages to a page frame, buttons to a button group, and so on. And, of course, you can remove the ones that are already there.

Usage

oObject.AddObject( cName, cClass [, cOLEClass ]
                   [, uParamList ] )
oObject.NewObject( cName, cClass [, cClassLib [, cLibInApp
                   [, uParamList ] ] ] )
oObject.RemoveObject( cName )

Parameter

Value

Meaning

cName

Character

The name of the object to be added or removed.

cClass

Character

The class of the object to be added.

cOLEClass

Character

If cClass is "OLEControl", specifies the type of OLE object to add.

uParamList

List of expressions

Parameters to be passed to the Init method of the new object.

cClassLib

Character

The name of the file containing the class definition for cClass.

Omitted

Either cClass is a VFP base class or the class library has already been opened with SET CLASSLIB or SET PROCEDURE.

cLibInApp

Character

The name (including extension) of an APP or EXE file containing cClassLib.

AddObject and NewObject (added in VFP 6) are similar, but NewObject doesn’t require SET CLASSLIB or SET PROCEDURE first. The difference between these two is quite similar to the difference between CREATEOBJECT() and NewObject(). We suspect, as in that case, that we’ll find ourselves using NewObject() more and more, once we remember to do so.

NewObject() also offers the opportunity to add objects based on classes built into APP or EXE files, without having to have the library available as a separate VCX or PRG.

At runtime, the object added to the container always has Visible set to .F., even if you explicitly set it in the object’s Init. This lets you do all the fiddling around you need with the new object before it becomes visible, so the user doesn’t see what’s happening. To ensure the display does not update until all changes are made, you can also toggle the form’s LockScreen property.

Because these methods work at design-time as well as runtime, they’re extremely useful in Builders. You can get the user’s input, add the appropriate objects and set them up the way the user wants. For more on Builders, see “Builders and Wizards and Bears, Oh My!” in the “But Wait, There’s More!” section.

Don’t confuse these methods with the ones used to add items to a list or combo box.

Example

oForm = CreateObject('Form')
oForm.Show()
* Choose one of the next two method calls.
* This version assumes that the library file is in the
* search path.
oForm.AddObject('cmdClose',"CloseButton")
* or you could avoid the issue this way:
oForm.NewObject('cmdClose',"CloseButton","Buttons")

oForm.cmdClose.Top = 100
oForm.cmdClose.Left = 50
oForm.cmdClose.Visible = .T.

* Assume you have the equivalent of this definition in a
* visual class in a file called Buttons.VCX
DEFINE CLASS CloseButton AS CommandButton

   Caption = "Close"

   PROCEDURE Click

   ThisForm.Release()

   ENDPROC
ENDDEFINE

See Also

AddItem, AddListItem, CreateObject, Define Class, Init, LockScreen, RemoveItem, RemoveListItem, Visible