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

Tobias Boege tobs at taboege.de
Fri Sep 10 23:17:30 CEST 2021


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

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


More information about the User mailing list