Search code examples
c#frameworksbackend

Panel not anchor to right


I try the development front end of the application. But I have one problem, the panel is not anchored to right, ... this panel is dynamic but I don't understand, why not work.

enter image description here

enter image description here

            FlowLayoutPanel flp = new FlowLayoutPanel();
            flp.Dock = DockStyle.Fill;
            flp.AutoScroll = true;
            flp.BackColor = Color.Blue;
            pnl.Controls.Add(flp);
            Panel pnl1 = new Panel();
            pnl1.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
            pnl1.Size = new Size(flp.Width - 18, 40);
            pnl1.BackColor = Color.FromArgb(224, 224, 209);
            pnl1.Margin = new Padding(8, 2, 0, 0);
            flp.Controls.Add(pnl1);

Can help me thanks.


Solution

  • How about the width of FlowLayoutPanel. I see you set the size of Panel base on the width of the flow layout

    pnl1.Size = new Size(flp.Width - 18, 40);
    

    Update: After get more information from comment. This is behavior of FlowLayoutPanel.

    https://learn.microsoft.com/en-us/dotnet/desktop/winforms/controls/how-to-anchor-and-dock-child-controls-in-a-flowlayoutpanel-control?view=netframeworkdesktop-4.8