[Gambas-user] How do I directly access the child controls in a panel container.

Tobias Boege taboege at ...626...
Sat Jan 10 19:37:08 CET 2015


On Sat, 10 Jan 2015, T Lee Davidson wrote:
> On 01/10/2015 11:08 AM, J?rn Erik M?rne wrote:
> >
> >> Hello,
> >>
> >>
> >>
> >> One can address a control on another form with fMain.txtHello.Text. This
> >> syntax also works for other container objects. I have a form with a panel
> >> control. There are several controls within the panel. To avoid naming
> >> conflicts I want to keep the panel controls separate from the parent form.
> >> Something like panel.txtEdit.Text would be great but, it does not work. I
> >> can get the "children" collection of the panel and get the control names.
> >> That does not help. I need to get to the control and use its properties and
> >> events. Is there a way to do this?
> >>
> >>
> >>
> >> Thanks
> >>
> > This is the way: Panel1.Children[1].Text
> >
> 
> The documentation for Container.Children says that it, "Returns a collection [...]" But it does not return a Collection (class).
> 
> That's too bad. The ability to do Panel1.Children["TextBox1"].Text would be nice.
> 

If Panel1's containing Form is FForm, then you can do

  FForm.Controls["TextBox1"]

to have this access pattern. Since for every non-Form control, there must be
a Form somewhere up in the parent chain, it is always an option to go up
Panel.Parent, Panel.Parent.Parent, etc. to find the *first* container which
is a Form and then use its Controls property as shown above.

Since all controls (recursively[*]) included in a form must have different
names, there are no name clashes by design.

[*] It is not so easy if your Form embeds other Forms. If you want to, say,
    list all control names in your form, you descend recursively from the
    Form through all containers and Print their children's names.

    In this process, you may not descend into containers which happen to be
    Forms because another Form is a brand new namespace and you don't want
    the new names which are in there.

To test whether a container is a form, you can use the Is operator[0].

Regards,
Tobi

[0] http://gambaswiki.org/wiki/lang/is

-- 
"There's an old saying: Don't change anything... ever!" -- Mr. Monk




More information about the User mailing list