Names of TextLabels (and other form elements) as variables

Post

Posted
Rating:
#1 (In Topic #1392)
Avatar
Regular
mandarin is in the usergroup ‘Regular’
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)?

Code (gambas)

  1. Public Sub Form_Open()
  2.  
  3.  
  4. For ii = 0 To 9
  5.   TextLabel(1 + ii).Visible = True
  6.  

(Tried it, doesn't function.)

Online now: No Back to the top

Post

Posted
Rating:
#2
Guru
BruceSteers is in the usergroup ‘Guru’
Use the Form or the Form.Conrols array

Ie…
The Form array

Code (gambas)

  1. Public Sub Form_Open()
  2.  
  3.  
  4. For ii = 1 To 10
  5.   Me["TextLabel" & ii].Visible = True
  6.  
  7.  
  8.  

or the Form.Controls array

Code (gambas)

  1. Public Sub Form_Open()
  2.  
  3.  
  4. For ii = 1 To 10
  5.   Me.Controls["TextLabel" & ii].Visible = True
  6.  
  7.  
  8.  
Online now: No Back to the top

Post

Posted
Rating:
#3
Avatar
Regular
mandarin is in the usergroup ‘Regular’
Thanks! It worked!

Online now: No Back to the top

Post

Posted
Rating:
#4
Avatar
Regular
mandarin is in the usergroup ‘Regular’
But, when I insert the line:

Code (gambas)

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

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

What now?

Online now: No Back to the top

Post

Posted
Rating:
#5
Guru
BruceSteers is in the usergroup ‘Guru’
 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]
Online now: No Back to the top

Post

Posted
Rating:
#6
Avatar
Regular
mandarin is in the usergroup ‘Regular’
Thanks, it worked properly.

Online now: No Back to the top

Post

Posted
Rating:
#7
Guru
BruceSteers is in the usergroup ‘Guru’
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,

Code (gambas)

  1.  
  2. Public Sub ControlObject(hControl As Control) As Object
  3.  
  4.   Return hControl
  5.  
  6.  

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]
Online now: No Back to the top
1 guest and 0 members have just viewed this.