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:
Starting the application with a lot of empty space... (design mode to show flowlayoutpanel size) - but almost the same ;)
Adding the very first control:
Thered
area is the flowlayoutcontrol, theblue
one is my usercontrol.Adding the 2nd control ends up in a flowlayout which have scrollbars (as i dont want)
Adding another control sizes the flowlayout pannel correctly (as i want)
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 toBottom, Right
In the click-eventhandler of the
add-button
insert this code:var myNewBlueControl = new MyUserControl1(); myNewBlueControl.Visible = true; this.panelToUse.Controls.Add(myNewBlueControl);