Setting Control names as arrays.

burd27

Member
Licensed User
A little while ago I read a Thread that wasn't specifically about this topic, but it did mention how to go about addressing Controls as arrays (eg. Ten Text boxes on the one form - a la Box(1), Box(2), ....)

I Believe that you can't do this in B4P, but in the thread that I mentioned above (which I cannot find again), I recall (Erel?) descibing how it can be achieved.

Can it be explained again or can I be pointed to the thread in question.

Big thanks.

Burd
 

willisgt

Active Member
Licensed User
There are two possibilities:

1.) Create your ten controls - let's use a textbox as an example - in the Designer. Name them textbox1, textbox2, textbox3, etc...

- or -

2.) Create the controls dynamically, like such...

B4X:
Sub App_Start

   l = 5
   t = 5
   w = Form1.Width - ( l * 2 )
   h = 15

   For x = 1 To 10

      controlname = "textbox" & x
      txt = "textbox number " & x

      AddTextBox( "Form1", controlname, l, t, w, h, txt )
      
      t = t + Control( controlname ).Height + 2
   
   Next

   Form1.Show

End Sub


In either case, you can reference the controls in a manner similar to this:

B4X:
Sub Button1_Click

   bkg = Rgb( Rnd( 0, 254 ), Rnd( 0, 254 ), Rnd( 0, 254 ) )
   fnt = Rgb( Rnd( 0, 254 ), Rnd( 0, 254 ), Rnd( 0, 254 ) )

   For x = 1 To 10
   
      controlname = "textbox" & x
   
      Control( controlname ).Color = bkg
      Control( controlname ).FontColor = fnt
   
   Next

End Sub

That's pretty much it!


Gary
 

specci48

Well-Known Member
Licensed User
Longtime User
As a small addition: If you want to use the control keyword in connection with "Optimized Compilation" you have to set the control type, too.

Instead of
t = t + Control( controlname ).Height + 2
...
Control( controlname ).Color = bkg
Control( controlname ).FontColor = fnt
you should use this
t = t + Control( controlname, TextBox ).Height + 2
...
Control( controlname, TextBox ).Color = bkg
Control( controlname, TextBox ).FontColor = fnt


specci48
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
The runtime controls tutorial should help you: http://www.b4x.com/forum/showthread.php?t=907

If you want to use the control keyword in connection with "Optimized Compilation" you have to set the control type, too.
With most properties (see Control keyword topic for the full list) it isn't required to add the control type.
The compiler will show a message when you try to compile if it is required.
 

specci48

Well-Known Member
Licensed User
Longtime User
With most properties (see Control keyword topic for the full list) it isn't required to add the control type.
:signOops: ... haven't realized this yet.
As I remeber I had to add the control type to all my used contol statments when using "optimized compilation" on my progs. By the way, it was a very stupid work... :sign0148:

specci48
 

derez

Expert
Licensed User
Longtime User
controls

Burd
I think that you refer to my question in a thread.
I wanted to refer to many controls with the same action - like to change their color.
The solution is like this:
Define an array, for example : Dim boxes()
then allocate each control to an item in this array using strsplit:
boxes() = StrSplit("Form1,Panel2,TimeData2,MTime,Mspeed,Alt2,.................,Routetime",",")

then the action is simple:
For i = 10 To 35
Control(boxes(i)).Color = Rgb(255,255,255)
Control(boxes(i)).fontColor = Rgb(0,0,0)
Next i

The advantage of this solution is that it keeps the specific names of the controls, which you want to refer specifically with actions or values, without having to look-up every time in a list to find the control you need.

and It saves a lot of text....:)

edit: I just noticed that I wrote "next i " instead of just "next" (old habit from other languages), and the compiler doesn't mind !
 
Last edited:

burd27

Member
Licensed User
What can I say.....

Thanks folks so much. Collectively, beautifully explained (I need that). This is a GREAT help and has saved me about 500 lines of code as well!

Not only is this info much appreciated, but also this forum in bringing out the benevolent side of "software developers" where ever we are on the scale...... oh,.... I think I;m going to cry.... = (

Burd.
 
Top