B4J Question [Solved] How to reference a series of labels

bdunkleysmith

Active Member
Licensed User
Longtime User
I have a form with a series of 64 labels with Id lbl0, lbl1, lbl2, . . . . , lbl61, lbl62, lbl63

I am struggling to work out how to reference each in a for/next loop via their Id in order to populate the label text:

B4X:
    Dim tmp As String
    For b = 63 To 0 Step -1
        tmp = "lbl" & b
        
'       How to reference labels with Id lbl63, lbl62, . . . . , lbl2, lbl1, lbl0 as we step through loop?

    Next
 

DonManfred

Expert
Licensed User
Longtime User
Not exactly what you want but maybe helpful


You can use a Map to map between the name and the Objects.
Add the objects - after you loaded the layout - to the map.
B4X:
dim m as Map
m.initialize
m.Put("lbl0", lbl0)
m.Put("lbl2", lbl2)
m.Put("lbl3", lbl3)
'
' Use
dim lbl as label = m.get("lbl0")

Dim tmp As String
    For b = 63 To 0 Step -1
        tmp = "lbl" & b
'       How to reference labels with Id lbl63, lbl62, . . . . , lbl2, lbl1, lbl0 as we step through loop?
        dim lbl as label = m.get(tmp)
       ' work with lbl....
    Next
 

Attachments

  • LabelReferenceTest.zip
    9.4 KB · Views: 160
Last edited:
Upvote 0

aeric

Expert
Licensed User
Longtime User
B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private lbl0, lbl1, lbl2, lbl3, lbl4, lbl5, lbl6, lbl7, lbl8, lbl9, lbl10 As B4XView
    Private Labels() As B4XView
End Sub

Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")

    Labels = Array As B4XView(lbl0, lbl1, lbl2, lbl3, lbl4, lbl5, lbl6, lbl7, lbl8, lbl9, lbl10)
    For i = 0 To Labels.Length - 1
        Labels(i).Text = "lbl" & i
    Next
End Sub
 
Upvote 0

Magma

Expert
Licensed User
Longtime User
Upvote 0

bdunkleysmith

Active Member
Licensed User
Longtime User
Thanks all for your suggestions which provided a selection of paths to take.

As it was a legacy project with the form created in SceneBuilder to which I just wanted to add some labels as a quick diagnostic tool, I finished up using this code to add the labels which I could reference:

B4X:
Private dataLabels(64) As Label

Sub AppStart (Form1 As Form, Args() As String

    Dim lblX As Int = 45
    Dim lblY As Int = 410
    For b = 63 To 0 Step -1
        dataLabels(b).Initialize("Label")
        dataLabels(b).Text = "1"
        dataLabels(b).Alignment = "CENTER"
        MainForm.RootPane.AddNode(dataLabels(b), lblX, lblY, 15, 15)
        lblX = lblX + 15   
    Next
    
End Sub

Sub ScoreboardText(Text As String)
    For b = 0 To 63
        dataLabels(63 - b).text = Text.SubString2(b, b + 1)
    Next
End Sub
 
Upvote 0
Top