Names of TextLabels (and other form elements) as variables

New to Gambas? Post your questions here. No question is too silly or too simple.
Post Reply
User avatar
mandarin
Posts: 16
Joined: Friday 29th November 2024 6:43am

Names of TextLabels (and other form elements) as variables

Post by mandarin »

Merry Christmas Ho Ho Ho!!! :D

Can I have TextLabels (or any other form elements) referred to with variable names?

To be precise, is it acceptable in Gambas3 to write a code somewhat like the following - after I named the TextLabels appropriately on the Form.form (starting from TextLabel1 / ending with TextLabel10)?

Public Sub Form_Open()

Dim ii As Integer

For ii = 0 To 9
  TextLabel(1 + ii).Visible = True
Next

End


(Tried it, doesn't function.)
User avatar
BruceSteers
Posts: 1972
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Names of TextLabels (and other form elements) as variables

Post by BruceSteers »

Use the Form or the Form.Conrols array

Ie...
The Form array
Public Sub Form_Open()
 
Dim ii As Integer
 
For ii = 1 To 10
  Me["TextLabel" & ii].Visible = True
Next
 
End



or the Form.Controls array
Public Sub Form_Open()
 
Dim ii As Integer
 
For ii = 1 To 10
  Me.Controls["TextLabel" & ii].Visible = True
Next
 
End

If at first you don't succeed , try doing something differently.
BruceS
User avatar
mandarin
Posts: 16
Joined: Friday 29th November 2024 6:43am

Re: Names of TextLabels (and other form elements) as variables

Post by mandarin »

Thanks! It worked!
User avatar
mandarin
Posts: 16
Joined: Friday 29th November 2024 6:43am

Re: Names of TextLabels (and other form elements) as variables

Post by mandarin »

But, when I insert the line:

Me.Controls["TextLabel" & ii].Text = some-string-out-of-an-array[ii]


halts the program, displaying: "TextLabel.Text is not a property".

What now?
User avatar
BruceSteers
Posts: 1972
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Names of TextLabels (and other form elements) as variables

Post by BruceSteers »

Because the Form array and the Form.Controls array only returns a "Control" type

Only Control.class properties can be accessed directly.
All controls inherit Control.class but all added properties (like .Text) have to be accessed another way.

Use either Object.class or the specific class you want like this...

Dim o As Object = Me.Controls["TextLabel" & ii]
o.Text = some-string-out-of an-array[ii]

or this...
Dim tl As TextLabel = Me.Controls["TextLabel" & ii]
tl.Text = some-string-out-of an-array[ii]
If at first you don't succeed , try doing something differently.
BruceS
User avatar
mandarin
Posts: 16
Joined: Friday 29th November 2024 6:43am

Re: Names of TextLabels (and other form elements) as variables

Post by mandarin »

Thanks, it worked properly.
User avatar
BruceSteers
Posts: 1972
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Names of TextLabels (and other form elements) as variables

Post by BruceSteers »

You're welcome.

Many of my programs have a simple function to treat a Control as an Object so i can easily access it's properties,


Public Sub ControlObject(hControl As Control) As Object

  Return hControl

End


So with that function I can simply use it to set any property of a Control.class return type
like this...

ControlObject(Me.Controls["TextLabel" & ii]).Text = some-string-out-of-an-array[ii]
ControlObject(Me.Controls["CheckBox" & ii]).Value = some-boolean-value-array[ii]
If at first you don't succeed , try doing something differently.
BruceS
Post Reply