WinForm for the most efficient layout code

A section of code (WinForm form):
Using a for loop to create x (x can be any number of) button,
Then use the most efficient code, the X button in the interface, from left to right, in order from top to bottom.
Best can enter the X in the interface, the distance between each button.
At the same time in the size of the form change, but also the most efficient from the new order.

Started by Dolores at February 15, 2016 - 4:58 PM

Keep before a coordinate, to create a press button position coordinate increment

Posted by Gloria at November 15, 2016 - 12:25 PM

See you when using for to create button, reminds me of his past to attain the highest level in one step of the ideas, or own a drag on, half an hour can drag with complete, you must use your own for said you estimate to get you cool is not good, I have personally tried, not to the water level don't think those technology

Posted by Paula at November 24, 2016 - 12:32 PM

Use flowLayoutPanel to load the button do not align themselves button.

Posted by Merlin at December 03, 2016 - 12:35 PM

You may think too complicated, I use for because I button is not determined, by reading the data inside the database, the data line represents a button. Then create the button added to the interface layout, this situation is not drag. I realized, but I think my code efficiency is too low.!

Posted by Dolores at December 16, 2016 - 1:11 PM

The tried, fast, but the distance between each button to set.?

Posted by Dolores at December 19, 2016 - 1:21 PM

You change the margin property to button to see the effect

Posted by Merlin at December 25, 2016 - 2:17 PM


        Panel p = new Panel();
        public Form4()
        {
            InitializeComponent();
            p.Dock = DockStyle.Fill;
            this.Controls.Add(p);
            this.SizeChanged+=new EventHandler(Form4_SizeChanged);
        }

        private void Form4_SizeChanged(object sender, EventArgs e)
        {
            this.p.Controls.Clear();
            int w = this.p.Width;
            int h = this.p.Height;
            int x = 0, y = 0;
            int cs = 5, vs = 3;
            int tb = 0;
            for (; y <h; )
            {
                Application.DoEvents();
                y += tb;
                for (; x <w; )
                {
                    Button b = new Button();
                    b.Padding = new Padding(cs, vs, 0, 0);
                    b.Location = new Point(x, y);
                    x += cs + b.Width;
                    if (vs + b.Height > tb)
                        tb = vs + b.Height;
                    if (x >= w) { x = 0; break; }
                    if (y + tb >= h) return;
                    this.p.Controls.Add(b);
                    Application.DoEvents();
                }
            }
        }

In fact, very fun, very obvious, need memory consumption is not large, CPU

Posted by Harlan at January 01, 2017 - 4:36 PM

 Panel p = new Panel();
        public Form4()
        {
            InitializeComponent();
            p.Dock = DockStyle.Fill;
            this.Controls.Add(p);
            this.SizeChanged+=new EventHandler(Form4_SizeChanged);
        }

        private void b_Click(object sender, EventArgs e)
        {
            MessageBox.Show(((Button)sender).Text);
        }
        private void Form4_SizeChanged(object sender, EventArgs e)
        {
            this.p.Controls.Clear();
            int w = this.p.Width;
            int h = this.p.Height;
            int x = 0, y = 0;
            int cs = 5, vs = 3;
            int tb = 0;
            int n = 0;
            for (; y <h; )
            {
                Application.DoEvents();
                y += tb;
                for (; x <w; )
                {
                    Button b = new Button();
                    b.Width = new Random().Next(30,100);
                    b.Click += new EventHandler(b_Click);
                    b.Padding = new Padding(cs, vs, 0, 0);
                    b.Location = new Point(x, y);
                    x += cs + b.Width;
                    if (vs + b.Height > tb)
                        tb = vs + b.Height;
                    if (x >= w) { x = 0; break; }
                    if (y + tb >= h) return;
                    n++; b.Text = n.ToString();
                    this.p.Controls.Add(b);
                    Application.DoEvents();
                }
            }

        }

Casual typesetting

Posted by Harlan at January 08, 2017 - 2:42 PM

How to set up a line of how many? For example, I fixed to 5 in each row, with the form drag, maximize, are only a row of 5.

Posted by Dolores at January 10, 2017 - 3:37 PM

The button distance between any change.?
If the changes it according to the above method, if not change, the set width of flowLayoutPanel does not vary with the form size change can be

Posted by Merlin at January 14, 2017 - 4:21 PM