1

I try to add some UserControls via code to a System.Windows.Forms.FlowLayoutPanel (.net 4.0).
I have set the .AutoSize and WrapContents -Property to True. I wanted to have each control in a new 'Row'.

At my Test-Form i have enough space to add several instances of my MyUserControl.
The Problem is:

For every 2nd control i add, both scrollbars appear at the FlowLayoutPanel.

Step by step:

  1. Starting the application with a lot of empty space... (design mode to show flowlayoutpanel size) - but almost the same ;)

    enter image description here

  2. Adding the very first control:
    The red area is the flowlayoutcontrol, the blue one is my usercontrol.

    2nd step - adding 1rst control

  3. Adding the 2nd control ends up in a flowlayout which have scrollbars (as i dont want)

    Scrollbars every 2nd control

  4. Adding another control sizes the flowlayout pannel correctly (as i want)

    Correct size after 2nd item (every 2nd)

I can go further like this. Repeatly the same problem.

Here are my codeparts:

Main-Form ctr:

PanelToUse.AutoSizeMode = AutoSizeMode.GrowAndShrink;
PanelToUse.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;  
PanelToUse.BackColor = System.Drawing.Color.Red;                           
PanelToUse.AutoSize = true;

Behind the ADD-Button:

 var myNewBlueControl = new MyUserControl1();
 myNewBlueControl.Visible = true;
 this.PanelToUse.Controls.Add(myNewBlueControl);

Any useful suggestion? What do i miss?
Thanks in advance!!

EDIT: Here are all codeparts to reproduce this issue:

Create a new WindowsForms-Project.

  • Add a new UserControl and rename this class to "MyUserControl1"
  • In the MyUserControl1-Ctr add the following code:

    this.Size = new Size(410, 100); this.BackColor = Color.Blue;

  • Add the following member to the Form1-Class:

    FlowLayoutPanel panelToUse;

  • Add this Code to the Form1-Constructor

            this.Size = new Size(1046, 367);
            this.MaximumSize = new Size(550, 700);
    
            panelToUse = new FlowLayoutPanel();
            this.Controls.Add(this.panelToUse);
            panelToUse.WrapContents = true;
            panelToUse.Location = new Point(10, 10);
            panelToUse.AutoSizeMode = AutoSizeMode.GrowAndShrink;
            panelToUse.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
            panelToUse.BackColor = System.Drawing.Color.Red;
            panelToUse.AutoSize = true;
            panelToUse.AutoScroll = true;
            panelToUse.Visible = true;
    
  • Than add a new Button to the very right corner and set its anchor to Bottom, Right

  • In the click-eventhandler of the add-button insert this code:

    var myNewBlueControl = new MyUserControl1(); myNewBlueControl.Visible = true; this.panelToUse.Controls.Add(myNewBlueControl);

Cadburry
  • 1,844
  • 10
  • 21
  • If you don't want it to scroll then `PanelToUse.AutoScroll = false`. Probably `PanelToUse.AutoSizeMode = AutoSizeMode.GrowOnly` as well. – DonBoitnott May 24 '16 at 15:15
  • http://stackoverflow.com/a/15792374/17034 – Hans Passant May 24 '16 at 15:33
  • @DonBoitnott Thanks for the hint, but i need the user to be able to scroll to a desired item because the form height will be limited... the items not – Cadburry May 25 '16 at 06:53
  • @HansPassant Oh! Really? A Bug? - I'l give it a try... thx! – Cadburry May 25 '16 at 06:53
  • @HansPassant Adding the dummy doesnt really helped in my case... Am also not sure if this issue really take effect in my case.. Do you know if this bug only applies to the flowLayout panel or for the tableLayoutPanel too? ty – Cadburry May 31 '16 at 08:22
  • You are not helping us help you, the linked post talks about a bug that's specific to the FlowBreak property. We can't tell if it applies to your case when you post screenshots instead of repro code. Note how the OP in the linked post did that right. Next best guess is seeing that the panel actually is big enough, if only it did not have the scrollbars. Next thing to try is wrap the Add() call with PanelToUse.Suspend/ResumeLayout(). – Hans Passant May 31 '16 at 09:35
  • Thank you - But there is not much more needed - Anyway i have edited my question and provided all code which is needed to reproduce my issue – Cadburry May 31 '16 at 11:04

0 Answers0