B4J Question Label Array

atiaust

Active Member
Licensed User
Longtime User
Hi All,

I have a form with 5 labels (Label1,Label2,Label3,Label4,Label5)

I want to be able to set the .text of each label within a loop and also how many labels to write to.

I found an example but it doesn't write the text to the label.

B4X:
Process Globals
    Private Label1 As Label
    Private Label2 As Label
    Private Label3 As Label
    Private Label4 As Label
    Private Label5 As Label
    Private lbls() As Label
End Sub

'
Sub AppStart (Form1 As Form, Args() As String)
'   
    lbls = Array As Label (Label1,Label2,Label3,Label4,Label5)
'    
    For i = 0 To lbls.Length -1
        lbls(i).Initialize("lbls")
        lbls(i).Tag = i + 1
        lbls(i).text = i + 1
    Next
'

Anyone know how to do it?

Thanks
 

DonManfred

Expert
Licensed User
Longtime User
I found an example but it doesn't write the text to the label.
You should load them into a layout first...
Or add the buttons to the form programatically.

Note that
- if you load a layout you should not use INITIALZE
- If you dont use a layout then you need to add the labels manually to the Activity(form)
 
Upvote 0

atiaust

Active Member
Licensed User
Longtime User
Thanks,

I got it to work by add the nodes programatically using:

B4X:
Dim j As Int = 360    'top
    For i = 0 To 4
        txtLbls(i).Initialize("lbls")
        MainForm.RootPane.AddNode(txtLbls(i),20,j,150,25)
        j = j+30
    Next

But I can't get it to work using labels added thru the designer.
 
Upvote 0

atiaust

Active Member
Licensed User
Longtime User
Hi Manfred,

Added labels onto layout in the designer.

B4X:
Sub Process_Globals  
    Private lbl1 As Label
    Private lbl2 As Label
    Private lbl3 As Label
    Private lbl4 As Label
    Private lbl5 As Label
    Private lbls() As Label

End Sub

'
Sub AppStart (Form1 As Form, Args() As String)
'   
    MainForm = Form1
    MainForm.RootPane.LoadLayout("lytMain") 'Load the layout file.
'
    lbls = Array As Label(lbl1,lbl2,lbl3,lbl4,lbl5)
'   
    For i = 0 To lbls.Length -1
        lbls(i).Initialize("lbls")
        lbls(i).Tag = i + 1
        lbls(i).text = i + 1
        lbls(i).TextSize = 11
    Next

When I run this each label shows "Text" instead off "1", "2" etc
 
Upvote 0

atiaust

Active Member
Licensed User
Longtime User
Yes correct. The initialize statement caused the problem.

lbls(i).Initialize("lbls")

Fixed

Thanks
 
Upvote 0
Top