Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Fun Code: Cloning Objects!
Message
 
To
All
General information
Forum:
Visual FoxPro
Category:
Other
Title:
Fun Code: Cloning Objects!
Miscellaneous
Thread ID:
00064532
Message ID:
00064532
Views:
67
I had a need to clone objects on the fly. Here is the code I came up with...
To try this out, create a form and place a textbox on the form. Change the
textbox's DragMode property to Automatic. Then, in the DragDrop method of
the form, place the code found at the bottom of this message. Just run the
form and drag-and-drop the textbox somewhere on the form. The code will
duplicate the object with all properties , even custom properties that are
not part of the class. For example, type in some text in the textbox at
runtime, drag-and-drop it, the text is also duplicated. The cloned
textboxes can also be cloned ad infinitum. What is great about this example
is that it allows you to clone an object on the fly, regardless of if the
object is a foxpro base class or a custom object with custom properties set.
If anyone is interested, I was researching this to be used with my searchable
textbox class to make it more versatile and generic.


****************************
LPARAMETERS oSource, nXCoord, nYCoord
LOCAL laClone, laExclude, lnSize, lnExclude, lnFound, luValue, lcObjName

** Create a public variable to hold the instance number.
** Faster than looking through objects for last instance number.
IF TYPE('gnInstance') <> 'N'
PUBLIC gnInstance
gnInstance = 0
ELSE
gnInstance=gnInstance+1
ENDIF
lcObjName = "txtDrag"+ALLTRIM(STR(gnInstance))

lnExclude = 10
lnFound = 0
DIMENSION laClone(1), laExclude(lnExclude)

** Set properties that are either read-only or that
** we do not wish to change from the default.
laExclude(1) = "CONTROLSOURCE"
laExclude(2) = "BASECLASS"
laExclude(3) = "CLASS"
laExclude(4) = "CLASSLIBRARY"
laExclude(5) = "NAME"
laExclude(6) = "PARENT"
laExclude(7) = "PARENTCLASS"
laExclude(8) = "TEXT"
laExclude(9) = "TOP"
laExclude(10) = "LEFT"

lnSize=AMEMBERS(laClone, oSource)

** Remove the properties we do not want to set
FOR i=1 TO lnExclude
lnFound = ASCAN(laClone, laExclude(i))
IF lnFound != 0
=ADEL(laClone, lnFound)
lnSize = lnSize - 1
lnFound = 0
ENDIF
ENDFOR

THIS.ADDOBJECT(lcObjName, oSource.CLASS)

** Set all the remaining properties
FOR i=1 TO lnSize
lcproperty = laClone(i)
luValue = oSource.&lcproperty
THIS.&lcObjName..&lcproperty = luValue
ENDFOR

THIS.&lcObjName..LEFT = nXCoord
THIS.&lcObjName..TOP = nYCoord
****************************

Michael G. Emmons
memmons@nc.rr.com
Next
Reply
Map
View

Click here to load this message in the networking platform