Order of dynamically placed controls.

Hans 251 Reputation points


I place a few panels and splitters dynamically on a form (Winforms).

What I want is panel1||splitter1||panel2||Splittter2. It seems that the order in which the panels are added with controls.add() is not respected.

what I get is: panel2||splitter1||panel1

How can I adjust the order?

private void button1_Click(object sender, EventArgs e)
            TabPageItems.Controls.Add(CreatePanel(1,DockStyle.Left, "test", Color.Blue));
            TabPageItems.Controls.Add(CreatePanel(2, DockStyle.Left, "test", Color.LightBlue));

        private Panel CreatePanel(int PanelNumber, DockStyle dockStyle, string panelType, Color color)
            // Create a dynamic panel
            Panel dynamicPanel = new()
                Name = panelType + PanelNumber.ToString(),
                Size = new Size(50, 50),
                Location = new Point(0, 0),
                BackColor = color,
                BorderStyle = BorderStyle.FixedSingle,
                Dock = dockStyle,
                AutoSize = false

            return dynamicPanel;

        private Splitter CreateSplitter(int SplitterNumber)
            Splitter splitter = new()
                Name = "SPLITTER_" + SplitterNumber.ToString(),
                Dock = DockStyle.Left,
                Width = 6,
                BackColor = Color.DeepSkyBlue,
                Location = new Point(0, 0),
A family of Microsoft operating systems that run across personal computers, tablets, laptops, phones, internet of things devices, self-contained mixed reality headsets, large collaboration screens, and other devices.
4,052 questions
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
9,437 questions
{count} votes

4 answers

Sort by: Most helpful
  1. KOZ6.0 4,015 Reputation points

    The specification is that a collection of controls is enumerated from front to back. You can customize Control.Collection to change the order in which windows are created, but this is not recommended as it confuses Form Designer.

    public class TabPageEx : TabPage
        protected override ControlCollection CreateControlsInstance() {
            return new ControlCollectionEx(this);
        public class ControlCollectionEx : Control.ControlCollection
            public ControlCollectionEx(Control owner) : base(owner) { }
            public override void Add(Control? value) {
    0 comments No comments

  2. Hans 251 Reputation points

    It looks like different controls don't match.

    This works, result is panel-splitter-panel:


    This does not work: result is splitter - panel - panel


  3. Hans 251 Reputation points

    Thanks, you were right. Now it works.

    0 comments No comments

  4. Hans 251 Reputation points
    0 comments No comments