Setting Control names as arrays.

Discussion in 'Questions (Windows Mobile)' started by burd27, Jan 28, 2008.

  1. burd27

    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.

  2. willisgt

    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...

    Sub App_Start

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

    For x = 1 To 10

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

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


    End Sub

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

    Sub Button1_Click

       bkg = Rgb( 
    Rnd0254 ), Rnd0254 ), Rnd0254 ) )
       fnt = Rgb( 
    Rnd0254 ), Rnd0254 ), Rnd0254 ) )

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

    End Sub
    That's pretty much it!

  3. specci48

    specci48 Well-Known Member Licensed 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
    you should use this

  4. Erel

    Erel Administrator Staff Member Licensed User

    The runtime controls tutorial should help you:

    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.
  5. specci48

    specci48 Well-Known Member Licensed User

    :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:

  6. derez

    derez Expert Licensed User


    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: Jan 28, 2008
  7. burd27

    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.... = (

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice