B4J Question Creating a label array on a form

Colin Evans

Active Member
Licensed User
Longtime User
Hi, another reference to good old VB6, creating a form in VB6 you could create labels or text fields using an array, so the first label would be call say Text1 and if you copied it and pasted the program would ask if you wanted to create an array

1622107573147.png


so the previous label would be Text1(0) and the copied one named Text1(1) and so on, this was to me, a brilliant aid in that when programming, for next loops were extremely easy to change the values of the created array i.e.

B4X:
for i = 0 to 10
  Text1(i).BackColor = &HFFFFFF
next i

The problem is I can't find a similar way of doing it in B4J so I have manually created the labels, say b1, b2, b3 etc. So if I wish to change the background color of all the labels I have to do them one by one, i.e.

B4X:
    Dim x As B4XView = b1
    x.Color = 0xFFFFFAFA
    Dim x As B4XView = b2
    x.Color = 0xFFFFFAFA
    Dim x As B4XView = b3
    x.Color = 0xFFFFFAFA
    Dim x As B4XView = b4
    x.Color = 0xFFFFFAFA
    Dim x As B4XView = b5
    x.Color = 0xFFFFFAFA
    Dim x As B4XView = b6
    x.Color = 0xFFFFFAFA
    Dim x As B4XView = b7
    x.Color = 0xFFFFFAFA
    Dim x As B4XView = b8
    x.Color = 0xFFFFFAFA
    Dim x As B4XView = b9
    x.Color = 0xFFFFFAFA
    Dim x As B4XView = b10
    x.Color = 0xFFFFFAFA

I assume there is a better way of coding this, again in my previous reincarnation using MSM (MUMPS) you could assign a new variable to use in the for next loop i.e.

B4X:
Dim var as string
for i = 1 to 10
     var = "b" & i
    Dim x As B4XView = @var
    x.Color = 0xFFFFFAFA
next i

Sadly I'm at a loss in B4J, can anyone help, thanks in advance
 

Andrew (Digitwell)

Well-Known Member
Licensed User
Longtime User
There are several ways of doing this.
one way is
B4X:
   For Each b4v As B4XView In Array(Label1,Label2,Label3,Label4,Label5,Label6,Label7,Label8) 
        b4v.TextColor = 0xFFFFFAFA
   Next

You can also define a list and add them or define an array of views with

B4X:
private lbls() as b4xview

or even
B4X:
private lbls() as list

lbls.initialize
lbls.add(Label1)  ' etc


and iterate over this array.
 
Upvote 0

Colin Evans

Active Member
Licensed User
Longtime User
There are several ways of doing this.
one way is
B4X:
   For Each b4v As B4XView In Array(Label1,Label2,Label3,Label4,Label5,Label6,Label7,Label8)
        b4v.TextColor = 0xFFFFFAFA
   Next

Thanks for providing a solution, it still would be nice to have the array created when designing the form as VB6 did, maybe Erel could work his magic as usual

Thank you once again, it's made life easier
 
Upvote 0
Top