Загрузка данных


using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        List<PictureBox> pics = new List<PictureBox>();
        Random r = new Random();

        public Form1()
        {
            InitializeComponent();

            int x = 0;
            int y = 0;
            int maxH = 0;

            for (int i = 0; i < 500; i++)
            {
                PictureBox p = new PictureBox();

                p.Width = r.Next(10, 40);
                p.Height = r.Next(10, 40);

                p.BackColor = Color.FromArgb(
                    r.Next(256),
                    r.Next(256),
                    r.Next(256));

                if (x + p.Width > 1000)
                {
                    x = 0;
                    y += maxH;
                    maxH = 0;
                }

                p.Location = new Point(x, y);

                panel1.Controls.Add(p);
                pics.Add(p);

                x += p.Width;

                if (p.Height > maxH)
                    maxH = p.Height;
            }
        }

        void DrawAgain()
        {
            panel1.Controls.Clear();

            int x = 0;
            int y = 0;
            int maxH = 0;

            foreach (PictureBox p in pics)
            {
                if (x + p.Width > 1000)
                {
                    x = 0;
                    y += maxH;
                    maxH = 0;
                }

                p.Location = new Point(x, y);

                panel1.Controls.Add(p);

                x += p.Width;

                if (p.Height > maxH)
                    maxH = p.Height;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            pics = pics.OrderBy(a => a.Width).ToList();

            DrawAgain();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            pics = pics.OrderBy(a => a.Height).ToList();

            DrawAgain();
        }
    }
}