[Gambas-user] How do I directly access the child controls in a panel container.
Tobias Boege
taboege at ...626...
Sat Jan 10 21:33:12 CET 2015
On Sat, 10 Jan 2015, T Lee Davidson wrote:
> > 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
> >
>
> Thanks for the elucidation, Tobi. But, I must be missing something.
>
> FForm.Controls["TextBox1"] returns a Control which does not have a Text property.
>
> .Parent ("Panel1.Parent") returns a Container which also does not have a Text property.
>
> If we do something like
>
> Dim hForm As Form
> If Panel1.Parent Is Form Then hForm = Panel1.Parent
>
> we would have hForm.Controls["TextBox1"], which returns a Control with no Text property.
>
It returns an object *typed* as a Control. But if the object is actually a
TextBox, say, you can "cast" that Control to a TextBox, with something like
Dim hControl As Control
Dim hTextBox As TextBox
hControl = FForm.Controls["TextBox1"]
If hControl Is TextBox Then
hTextBox = hControl
' ...
Endif
Since now the type of the hTextBox variable says "TextBox", and the object
behind it has a Text property, this property can successfully be resolved.
Note that for this trick to access non-Control properties, say Text, of an
object typed as Control, you need a class which is an ancestor (in the
inheritance tree) of the true class of that object and which also has that
property. It does not suffice to pick an ancestor without this property or
to pick a random class which also happens to have a Text property but is not
an ancestor. (Like you cannot cast a Control which is actually a TextBox to
a ComboBox in order to access its Text.)
As for the hijacking, my bad. But I didn't understand Martin's question
either way, so let's pretend I thought this was relevant to the thread :-)
Regards,
Tobi
--
"There's an old saying: Don't change anything... ever!" -- Mr. Monk
More information about the User
mailing list