Android Question [Solved] How I can change the text property of several labels using a for..next?

asales

Expert
Licensed User
Longtime User
I have several labels with this names lb1, lb2, lb3.. lb33.

How I can access and change the text property, automatically, of each label using a for..next like this example:
B4X:
For i = 1 To 7
    lb&i.text = i
Next

Thanks in advance for any tip.
 

walterf25

Expert
Licensed User
Longtime User
I have several labels with this names lb1, lb2, lb3.. lb33.

How I can access and change the text property, automatically, of each label using a for..next like this example:
B4X:
For i = 1 To 7
    lb&i.text = i
Next

Thanks in advance for any tip.
Instead of having lb1, lb2 etc.... you can define them as an array()

like this....

B4X:
Dim lbls(7) as label

for i = 0 to lbls.lenght - 1
  lbls(i).text = i
next

Walter
 
Upvote 0

asales

Expert
Licensed User
Longtime User
Thanks @walterf25, but don't work.

The labels was insert in Designer.

See this code (with your tip):

B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Private Label1 As Label
    Private Label3 As Label
    Private Label5 As Label
    Private Label7 As Label
    Private Label9 As Label
    Private Label10 As Label
    Private Label8 As Label
    Private Label6 As Label
    Private Label4 As Label
    Private Label2 As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("Layout1")

    Dim Label(11) As Label

    For i = 1 To 10
          Label(i).Initialize("")
          Label(i).text = i
    Next
End Sub
 
Upvote 0
Top