[Gambas-user] Dynamic Objects

Doriano Blengino doriano.blengino at ...1909...
Mon Mar 30 10:51:45 CEST 2009


Rodney Rundstrom ha scritto:
> Thanks to some of the member I can now create objects dynamically.
>
> If I create a number of buttons I can position and label them differently. I
> can also name them differently
>
> However I can have a number of objects with the same name on a form?
>
> I can access an event say click on any of the button but cannot write to a
> property of individual labels even using its created name
>
> being told it is not static.
>   
When you create controls at design time, those controls obtain a field 
(or slot) in the compiled code; ie, they have a true name because they 
become members of the class you are creating - both the Name property of 
the control, and a variable named the same, are retained.

When you create controls at run time, the class is already formed (it 
contains all the controls created at design time, but nothing else). So, 
they don't have a name in the class, and you can't access them that way. 
A control created in this way may or may not have a name assigned to its 
Name property - it is used for other purposes.

But you can still access them by saving/keeping the variable you used to 
instanciate them, either as a global one, a class member one (public or 
private), or inside an array, or whatever:

    private lMyLabel as Label

    lMyLabel = new Label(fmMain) as "another"

in this case, "another" is the name used for event management, but the 
real handle of the label will be "lMyLabel", and you can access 
"lMyLabel.text" in the usual way. But if lMylabel was DIMed as a local 
variable in a subroutine, then its handle gets lost after the subroutine 
terminates.

You can access controls in two ways, even if their handle is lost. The 
first is using "LAST" pseudo-variable in a event handler; the second is 
to use the Children property of a container control.

Regards,
Doriano Blengino





More information about the User mailing list