Order of dynamically placed controls.

Hans 251 Reputation points
2023-11-04T17:26:28.3+00:00

Hi,

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(CreateSplitter(1));
            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),
            };
Windows
Windows
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
C#
C#
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
    2023-11-04T19:13:54.5966667+00:00

    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) {
                base.Add(value);
                value?.BringToFront();
            }
        }
    }
    
    0 comments No comments

  2. Hans 251 Reputation points
    2023-11-04T19:44:52.6566667+00:00

    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
    2023-11-05T07:48:33.8933333+00:00

    Thanks, you were right. Now it works.

    
    
    0 comments No comments

  4. Hans 251 Reputation points
    2023-11-05T07:53:28.1666667+00:00
    
    
    0 comments No comments