How to invert Control.Controlcollection’s list of controls?

Asked

Viewed 30 times

2

I am inserting in a Panel several Labels, as I am inserting, the first Label becomes last in Panel. But I wanted to reverse this process. I wanted the first Label that was inserted to be in the first place in the Panel, and not in the 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);
}

1 answer

2


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

for(int i = 12; i >= 0; i--)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.