Loading controls with For loop

Hello,
Thanks to everyone who is kind enough to answer... Is there any way to load data into controls of a form by a For loop What i mean is: if i have 10 labels, can i load texts from a string array of 10, each string from the array to a corresponding label, by a loop
Thank You



Answer this question

Loading controls with For loop

  • SpaceDog

    If you don't want to create the control dynamically.

    Name each label as
     dynlabel0
     dynlabel1
     dynlabel2
     ...

    In you load

     Dim ctrl As Control

     For Each ctrl In Controls
      If ctrl.Name.StartWith("dynlabel") Then
       ''** ctrl is your label, you coud do Interger.Parse(ctrl.Name.Remove("dynlabel")) to get the ID
      End If
     Next

    (This was written in a text file, there might be compilation error)



  • Inray

    It appears that the website is changing the code example to contain emoticons - not great! Here it is again...




    public void loopThoseLabels(string[] arrayOfLabels)
    {
     for (int i = 0; i < arrayOfLabels.Length; i ++)
     {
                    System.Windows.Forms.Label newLabel = System.Windows.Forms.Label() ;
      newLabel.Text = arrayOfLabels//emoticons/emotion-55.gif" alt="Idea" /> ;
     }
    }


     




  • Colin Osbaldeston

    Yes!

    You can either create the labels dynamically depending on how many you are going to need, or assign them at runtime to pre-existing examples.

    Dynamically it would look something like this:




    public void loopThoseLabels(string[] arrayOfLabels)
    {
     for (int i = 0; i < arrayOfLabels.Length; i ++)
     {
                    System.Windows.Forms.Label newLabel = System.Windows.Forms.Label();
      newLabel.Text = arrayOfLabels//emoticons/emotion-55.gif" alt="Idea" />;
     }
    }


     



    You would need to position the labels yourself, using the top and left properties.

    All the best!



  • psygone13

    If the Labels or references to the labels are in an array then yes.

  • Loading controls with For loop