[Gambas-user] Impossible to get the text property of a textbox in a for each enumeration

Bruce Steers bsteers4 at gmail.com
Fri Sep 10 23:36:34 CEST 2021


On Fri, 10 Sept 2021 at 22:25, Marc Guillaume <alarch at alarch.pw> wrote:

> Le Fri, 10 Sep 2021 23:17:30 +0200,
> Tobias Boege via User <user at lists.gambas-basic.org> a écrit :
>
> > On Fri, 10 Sep 2021, Marc Guillaume wrote:
> > > Hello,
> > > I am retrieving data from a mysql database in a gridview. I edit a
> > > line with a popup menu in a form which contains the data of the
> > > database. The data in the form are in textboxes associated with a
> > > label and all these couples label textbox are in a panel. To be
> > > able to detect if the user has modified a field I put the database
> > > value in textbox1.text and textbox1.tag and so on. When closing the
> > > form I want to check if the user has modified any fields. So I
> > > browse the children of the panel and if the child is a textbox I
> > > want to compare the text and tag values. If they are different then
> > > I will know that there has been a modification and I can alert the
> > > user. So I put this code in the closure processing.
> > >
> > > Public Sub btnCancel_Click()
> > >
> > >   Dim iResponse As Integer
> > >   Dim oChild As Control
> > >   Dim bModif As Boolean = False
> > >
> > >   For Each oChild In panIel1.Children
> > >     If oChild Is TextBox
> > >       If oChild.text <> oChild.Tag
> > >         bModif = True
> > >       Endif
> > >     Endif
> > >   Next
> > >   If bModif
> > >     iResponse = Message.Warning( etc.
> > >
> > > But while in the for each loop I get the value of the tag without
> > > any problem I have an error for the text property :
> > >
> > > "Unknown text symbol in the control class"
> > >
> >
> > In this case, it matters for property lookup which type your variable
> > is declared as. You can look up the Tag property of oChild because it
> > exists in the Control class, but Text does not. Even if oChild is
> > really a TextBox, you declared it as merely a Control.
> >
> > You can either force a dynamic symbol lookup by using
> >
> >   If oChild Is TextBox Then
> >     If Object.GetProperty(oChild, "Text") <> oChild.Tag Then
> >       ...
> >
> > or by coercing oChild into a TextBox-typed variable:
> >
> >   If oChild Is TextBox Then
> >     Dim tbChild As TextBox = oChild
> >     If tbChild.Text <> tbChild.Tag Then
> >       ...
> >
> > The coercion will fail at runtime if the real class of oChild is
> > incompatible with (i.e. does not derive from) TextBox, but you guard
> > for that with the enclosing If ... Is TextBox.
> >
> > Best,
> > Tobias
> >
>
> Thank you very much!
>
> I thought there was a problem with classes and class inheritance but I
> didn't know how to access these textbox properties from the control class.
>

Another good trick if the type of object may vary is to use Object class
not Control.
Ie...
*  Dim oChild As Object*
Control class will only let you use properties that exist in Control but
Object class will let you access any existing property.
The downside is that Auto-complete will not pop a list relevant to your
control type but if the properties exist they will get accessed.

BruceS
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.gambas-basic.org/pipermail/user/attachments/20210910/93569a14/attachment-0001.htm>


More information about the User mailing list