Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamically adding objects to a form...

Status
Not open for further replies.

dal98

Technical User
Nov 17, 2001
9
US
Hi,

I'm struggling with a very simple concept and I'm sure somebody here knows what I'm doing wrong. I want to programmatically create an array of mycommandbuttons in a form. I tried from Init in my form:

dimension mybuttons(30)
for x = 1 to 30
mybutton(x) = createobject(mycommandbutton)
endfor

which works just fine except it isn't attached to the form so it isn't visible. If I addobject(), my array isn't recognized because it is defined in Init(). I even tried using this.addobject without an array to dynamically create 30 different button names, but I couldn't get my parameters passed to the class, and if I dynamically create a button name I can't really access it again to change the parameters after the object is created... What am I doing wrong? Or is there a better way to do this? BTW, I do need these buttons to be created dynamically...

Thanks, Derek
 
Derek,
Create a Form property mybuttons[30]. Then, assuming mycommandbutton has the classname of a command button class, in the Init() do:
for x = 1 to 30
lcName = "mycmd"+transform(x,"@L 99")
ThisForm.mybutton(x) = ;
ThisForm.addobject(lcName, mycommandbutton)
endfor

Rick
 
Using the addobject, what if you make the array a form property instead of creating it in the init(). This will make it persistent as long as the form is open. I don't know if this will work but it seems to me it should.
 
You could do something like this, as well:
Code:
PROCEDURE Init

FOR x=1 TO 30
	cName="DynCommand"+alltrim(str(x))
	this.addobject(cName,"commandbutton")
ENDFOR
After this runs, you will have 30 command buttons which can be referenced as Thisform.DynCommand1 through Thisform.DynCommand30.

Ian
 
Thanks all for the replies,

Using your advice I was able to create the objects as I wanted to. But now, a couple days later, I have decided that I would like to reference these dynamically created buttons.

I essentially followed Rick's advice so my button names are thisform.mycmd01,thisform.mycmd02... and I have an form property mybuttons(30). I now want to do something like:

mycmdxx.caption="blabla"
mycmdxx.refresh

where "xx" denotes a number from 1 to 30. The problem, of course, is the number of the button I need to access is stored in a variable. Can I use the mybuttons() array to access the object? Or is there some way to textually create the reference ?

Derek
 
Yes. Just create a character variable that contains the name of the control and use macro substitution:
[tt]
nButtonNum=5
cCmd2Use="mycmd"+alltrim(str(nButtonNum))
&cCmd2Use..Refresh()
[/tt]
It is necessary to put 2 periods because the first only signals the end of the macro name.

Ian
 
Thanks Ian !

Just what I was looking for. I thought macro might be the way to go, but I had no idea how to use it with an object.

Derek
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top