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.

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

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

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

    End Sub
    That's pretty much it!


    Gary
     
  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

    specci48
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    The runtime controls tutorial should help you: http://www.basic4ppc.com/forum/showthread.php?t=907

    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:

    specci48
     
  6. derez

    derez Expert Licensed 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: 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.... = (

    Burd.
     
Loading...