How to invert control list of Control.ControlCollection?

2

I am inserting in a Panel several Labels, as I am inserting, the first Label becomes last in the Panel. But I wanted to reverse this process. I wanted the first Label that was inserted, to be first in the Panel, and not last.

private void Form1_Load(object sender, EventArgs e)
{
    Label lbl;
    Panel pn = new Panel();
    pn.AutoScroll = true;
    pn.Dock = DockStyle.Fill;
    for (int i = 0; i < 13; i++)
    {
        lbl = new Label();
        lbl.Dock = DockStyle.Top;
        lbl.Text = i.ToString();
        pn.Controls.Add(lbl);
    }
    this.Controls.Add(pn);
}
    
asked by anonymous 19.08.2018 / 19:23

1 answer

2

Assuming that what identifies / distinguishes the Label is its text ( lbl.Text = i.ToString(); ), then just "flip" for :

for(int i = 12; i >= 0; i--)
    
19.08.2018 / 21:47