locked
how to run Label (the toolbox control) on loop coding RRS feed

  • Question

  • How to add the new Label Control on loop 

    like on for loop..

    • Moved by CoolDadTx Sunday, September 25, 2011 11:56 PM Winforms related (From:Visual C# General)
    Sunday, September 25, 2011 7:55 PM

Answers

  • Try this:

                Label[] labels;
                labels = new Label[5]; //5 lables created
                int x=10;
                int y=10;
                for (int i = 0; i < labels.Length; i++)
                {
                    labels[i] = new Label();
                    labels[i].Location = new Point(x, y);
                    labels[i].Text = "label" + i + 1;
                    this.Controls.Add(labels[i]);
                    y += 20;//set new location for the next label
                }
    



    Mitja
    • Proposed as answer by Thorsten Gudera Sunday, September 25, 2011 9:30 PM
    • Marked as answer by Neddy Ren Wednesday, September 28, 2011 8:17 AM
    Sunday, September 25, 2011 8:16 PM

All replies

  • Try this:

                Label[] labels;
                labels = new Label[5]; //5 lables created
                int x=10;
                int y=10;
                for (int i = 0; i < labels.Length; i++)
                {
                    labels[i] = new Label();
                    labels[i].Location = new Point(x, y);
                    labels[i].Text = "label" + i + 1;
                    this.Controls.Add(labels[i]);
                    y += 20;//set new location for the next label
                }
    



    Mitja
    • Proposed as answer by Thorsten Gudera Sunday, September 25, 2011 9:30 PM
    • Marked as answer by Neddy Ren Wednesday, September 28, 2011 8:17 AM
    Sunday, September 25, 2011 8:16 PM
  • Hi Prathamesh,s4,

    Welcome to the MSDN Forum.

    I think Mitja Bonca has given you the perfect samples for you.  So, I help you and mark the reply as an answer.

    If you have any questions, please feel free to tell us.

    Best Regards


    Neddy Ren [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, September 28, 2011 8:20 AM