如何让FlowLayoutPanel.AutoSize与FlowBreak一起使用

Nat*_*lia 12 c# flowlayoutpanel winforms

我有一个FlowLayoutPanel的问题,我不知道如何解决它.

我将两个FlowLayoutPanel放在另一个内; 第二个内部flp里面有3个按钮.

在此输入图像描述

FlowLayoutPanel子元素的属性是:

FlowDirection = LeftToRight;
AutoSize = true;
AutoSizeMode = GrowAndShrink;
WrapContents = true;
Run Code Online (Sandbox Code Playgroud)

现在我为每个按钮设置FlowBreak属性为true,但是我看到的行为不是我想要的,我希望FlowLayoutPanel缩小到按钮的宽度,

在此输入图像描述

更改FlowDirectionUpToDown是不是一种选择.

任何人都知道为什么AutoSize不起作用?

这是代码.

//
//FlowLayoutPanel1
//
this.FlowLayoutPanel1.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
this.FlowLayoutPanel1.Controls.Add(this.FlowLayoutPanel3);
this.FlowLayoutPanel1.Location = new System.Drawing.Point(84, 77);
this.FlowLayoutPanel1.MinimumSize = new System.Drawing.Size(10, 10);
this.FlowLayoutPanel1.Name = "FlowLayoutPanel1";
this.FlowLayoutPanel1.Size = new System.Drawing.Size(308, 265);
this.FlowLayoutPanel1.TabIndex = 0;
//
//FlowLayoutPanel3
//
this.FlowLayoutPanel3.AutoSize = true;
this.FlowLayoutPanel3.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
this.FlowLayoutPanel3.Controls.Add(this.Button1);
this.FlowLayoutPanel3.Controls.Add(this.Button2);
this.FlowLayoutPanel3.Controls.Add(this.Button3);
this.FlowLayoutPanel3.Location = new System.Drawing.Point(127, 3);
this.FlowLayoutPanel3.MinimumSize = new System.Drawing.Size(10, 10);
this.FlowLayoutPanel3.Name = "FlowLayoutPanel3";
this.FlowLayoutPanel3.Size = new System.Drawing.Size(162, 87);
this.FlowLayoutPanel3.TabIndex = 1;
//
//Button1
//
this.FlowLayoutPanel3.SetFlowBreak(this.Button1, true);
this.Button1.Location = new System.Drawing.Point(3, 3);
this.Button1.Name = "Button1";
this.Button1.Size = new System.Drawing.Size(75, 23);
this.Button1.TabIndex = 0;
this.Button1.Text = "Button1";
this.Button1.UseVisualStyleBackColor = true;
//
//Button2
//
this.FlowLayoutPanel3.SetFlowBreak(this.Button2, true);
this.Button2.Location = new System.Drawing.Point(3, 32);
this.Button2.Name = "Button2";
this.Button2.Size = new System.Drawing.Size(75, 23);
this.Button2.TabIndex = 1;
this.Button2.Text = "Button2";
this.Button2.UseVisualStyleBackColor = true;
//
//Button3
//
this.Button3.Location = new System.Drawing.Point(3, 61);
this.Button3.Name = "Button3";
this.Button3.Size = new System.Drawing.Size(75, 23);
this.Button3.TabIndex = 2;
this.Button3.Text = "Button3";
this.Button3.UseVisualStyleBackColor = true;
Run Code Online (Sandbox Code Playgroud)

Han*_*ant 15

这是一个错误,它已经存在很长一段时间了.问题是FlowLayoutPanel的布局引擎计算第一行的宽度错误,包括第二个控件的宽度,即使它被包装到第二行.

解决方法很愚蠢但有效,在第一个控件之后添加一个宽度为0的虚拟面板.如果您正在与设计师一起执行此操作,请先将其放下并将其拖动到第一个控件右侧的正确位置.然后在"属性"窗口中将其边距设置为(0,0,0,0),将大小设置为(0,0).


Mic*_*l G 6

我不相信FlowLayoutPanel是为了做你想做的事而设计的.TableLayoutPanel可能更适合.使用单个列添加TableLayoutPanel,并将每个按钮添加到一行.

编辑:我发现了一个hackish工作.在第一个按钮之后,创建一个大小为0,0且边距为0,0 的Panel.确保将FlowBreak设置为false.

在此输入图像描述

编辑:您只需要在第一个按钮之后创建一个面板,而不是每个按钮创建一个面板.