Where did Control Arrays go in VB.NET?

OK, I'm lost.  How do you iterate through a specific set of controls

Ex, in VB6, you would do the following:
For Ea = 0 To 3
     TxtBox(Ea)= "Test" & Ea
     lblLabel(Ea) = "Label" & Ea
Next

Now, in VB.NET the only way seems to do:
TxtBox0.Text = "Test0"
lblLabel0.Text = "Label0"
TxtBox1.Text = "Test1"
lblLabel1.Text = "Label1"
TxtBox2.Text = "Test2"
lblLabel2.Text = "Label2"
TxtBox3.Text = "Test3"
lblLabel3.Text = "Label3"

There has GOT to be a better way. Any help would be much appreciated.

Thanks,
Denvas


Answer this question

Where did Control Arrays go in VB.NET?

  • Sallu

    Nevermind - I figured out. Thanks anyway.
  • Esteban Garcia

    Control arrays were a part of the VB6 designer, but they didn't really work in Windows Forms (you were limited to a single control type in a Control Array, and Windows Forms need to be more flexible). If you need an aray of controls, you can create one yourself, adding instances of controls as necessary to that array.  Then, you can iterate through the data structure however you like. Every control has a Controls property, although most controls don't contain other controls, if that helps...
  • VanP

    To iterate through ALL controls on a form (including child controls):

    public void dbgprntControls(Control mainCntrl)
    {
    //System.Windows.Forms.Control ctrl = new Control() ;
    foreach (Control ctrl in mainCntrl.Controls )
    {
    Debug.Write("name:");
    Debug.Write(ctrl.Name);
    Debug.Write('\t');
    Debug.Write("tabidx");
    Debug.Write(ctrl.TabIndex);
    Debug.Write('\t');
    Debug.Write("tabstp:");
    Debug.Write(ctrl.TabStop);
    Debug.Write('\t');
    Debug.Write("text:");
    Debug.Write(ctrl.Text);
    Debug.Write('\t');
    Debug.Write("tag:");
    Debug.Write(ctrl.Tag);
    Debug.WriteLine("");
    if (ctrl.HasChildren )
    {
    dbgprntControls(ctrl);
    }
    }

    Obviously, do whatever instead of debug.write - I'm in the process of using this to initialize properties of edit controls on my forms to read the tag property and set edit masks as set at design time, and/or persisted in a data dictionary or perhaps via Keys.
    Anyway hope this helps.

  • Ben Riga

    Thanks! That's what I ended up doing, but at least I now know I did it right, and there wasn't an easier way I didn't know about.

    Much appreciated,
    Denvas

  • Where did Control Arrays go in VB.NET?