Share via

Sequence of dynamically created components

Hans 251 Reputation points
2024-12-14T13:10:09.9233333+00:00

Hi,

I'm trying to figure out the following

I have a split container. I then dynamically create a number of panels and splitters. The first panel created should end up in splitcontainer.panel1. (That's going well). All subsequent panels and splitters must end up in splitcontainer.panel2. The result should then be panel_2 | splitter1 | panel2 | splitter2 | panel3.docked = filled

For some reason things go wrong as soon as the last panel with docked is filled is placed. then the splitters move all the way to the left. Can this be made?

private void CreateBodyPanelsAndSplitters(Panel panelLeft, Panel panelRight)
{
for (int i = 1; i <= _numberOfPanels; i++)
{
    if (i == 1) // left splitcontainer panel
    {
        Panel p = CreatePanel(i, DockStyle.Fill, _panelBodyName, Color.AliceBlue);
        panelLeft.Controls.Add(p);                    
    }
    else // right splitcontainer panel
    {
        if (i < _numberOfPanels)
        {
            Panel p = CreatePanel(i, DockStyle.Fill, _panelBodyName, Color.AliceBlue);
            panelRight.Controls.Add(p);

            Splitter s = CreateSplitter(i);
            panelRight.Controls.Add(s);
        }
        else
        {
            // Last panel docked = fill
               Panel p = CreatePanel(i, DockStyle.Fill, _panelBodyName, Color.AliceBlue);
               panelRight.Controls.Add(p);
        }                    
    }
}
}

private static Panel CreatePanel(int panelNumber, DockStyle dockStyle, string panelType, Color color)
{
// Create a dynamic panel
Panel dynamicPanel = new()
{
    Name = panelType + panelNumber.ToString(),
    Size = new Size(100, 50),
    Location = new Point(10, 10),
    BackColor = color,
    BorderStyle = BorderStyle.Fixed3D, //BorderStyle.FixedSingle,
    Dock = dockStyle,
    AutoSize = false,
};

return dynamicPanel;
}
private static Splitter CreateSplitter(int splitterNumber)
{
Splitter splitter = new()
{
    Name = "SPLITTER_" + splitterNumber.ToString(),
    Dock = DockStyle.Left,
    Width = 3,
    BackColor = Color.Blue,
    Location = new Point(0, 0),
};

return splitter;
}

Windows for business | Windows Client for IT Pros | User experience | Other
Developer technologies | C#
Developer technologies | 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.

0 comments No comments

1 answer

Sort by: Most helpful
  1. Jiachen Li-MSFT 34,241 Reputation points Microsoft External Staff
    2024-12-16T06:33:30.1333333+00:00

    Hi @Hans ,

    The Dock property takes effect based on the z-order of controls in the Controls collection. When you add a new panel with DockStyle.Fill after the splitters and other panels, it might cause other docked elements (e.g., DockStyle.Left splitters) to be pushed or misaligned.

    Splitters need to be correctly placed between panels with DockStyle.Left. If the last panel is DockStyle.Fill, it can disrupt the layout because DockStyle.Fill expands to occupy any remaining space, pushing other controls unexpectedly.

    Try using DockStyle.Left for intermediate panels and splitters, and DockStyle.Fill for the final panel.

    private void CreateBodyPanelsAndSplitters(Panel panelLeft, Panel panelRight)
    {
        for (int i = 1; i <= _numberOfPanels; i++)
        {
            if (i == 1) // Left split container panel
            {
                Panel p = CreatePanel(i, DockStyle.Fill, _panelBodyName, Color.AliceBlue);
                panelLeft.Controls.Add(p);
            }
            else // Right split container panel
            {
                if (i < _numberOfPanels)
                {
                    // Intermediate panels and splitters
                    Panel p = CreatePanel(i, DockStyle.Left, _panelBodyName, Color.AliceBlue);
                    Splitter s = CreateSplitter(i);
    
                    // Add the splitter before the panel to ensure correct layout
                    panelRight.Controls.Add(p);
                    panelRight.Controls.Add(s);
                }
                else
                {
                    // Last panel with DockStyle.Fill
                    Panel p = CreatePanel(i, DockStyle.Fill, _panelBodyName, Color.AliceBlue);
                    panelRight.Controls.Add(p);
                }
            }
        }
    }
    

    Best Regards.

    Jiachen Li


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.