如何使用FlowBreak使FlowLayoutPanel.AutoSize正常工作

13

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

我在另一个FlowLayoutPanel中放置了两个FlowLayoutPanel;第二个内部FlowLayoutPanel中有3个按钮。

enter image description here

FlowLayoutPanel子项的属性为:

FlowDirection = LeftToRight;
AutoSize = true;
AutoSizeMode = GrowAndShrink;
WrapContents = true;

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

enter image description here

FlowDirection更改为UpToDown不是一个选择。

有人知道为什么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;

这是我的GUI中的问题一个控件https://docs.google.com/document/d/1I6OtboresNk-gfOR3sEM8gyYHVX9sGohdNtX6heeQwI/edit?usp=sharing当我放置两个控件并将FlowBrake设置为True时https://docs.google.com/document/d/17C02PoL8LCyymfXNtEP8N6kkzZETxkiOCb6mPTbzGf0/edit?usp=sharing我希望控件保持在上方,但随着宽度变化而不适合。 - Natalia
请问您能否添加两张图片——一张是您目前拥有的,另一张是您想要实现的。从文本描述中无法清楚地了解您所面临的问题。 - Sergey Berezovskiy
我添加了一张新图片并在我的上一条评论中放置了两个链接,展示了我的GUI的行为。 - Natalia
为什么将方向更改为 TopDown 不是一个选项? - Victor Zakharov
问题简述:为什么使用AutoSize时,布局面板右侧会保留足够的空间,就好像它要适应所有现有按钮中最大的那个一样? - Victor Zakharov
显示剩余2条评论
3个回答

17

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

解决方法很愚蠢但有效,可以在第一个控件之后添加一个宽度为0的虚拟面板。如果您使用的是设计器,请先将其放入正确的位置,即在第一个控件的右侧,然后在属性窗口中将其边距设置为(0, 0, 0, 0),大小设置为(0, 0)。


6
我不认为FlowLayoutPanel的设计可以实现您尝试做的事情。TableLayoutPanel可能更适合。添加一个具有单列的TableLayoutPanel,并将每个按钮添加到一行中。
编辑:我找到了一个hackish解决方法。在第一个按钮之后,创建大小为0,0且边距为0,0的面板。确保FlowBreak设置为false。
编辑:您只需要在第一个按钮后创建一个面板,而不是每个按钮都要创建一个面板。

1
同样的想法,同时出现。点赞... :) - Victor Zakharov
1
你的“hackish work around”太棒了。我也试图找到类似的方法,但是Panel控件没有出现在我的脑海中。真希望我能再给你一个赞。 - Victor Zakharov

2

这并不是一个解决方案,而是一个变通方法。看起来你试图通过在 FlowLayoutPanel 中使用流中断来模拟 TableLayoutPanel 的行为。你尝试过使用 TableLayoutPanel 吗?根据你评论中的截图,它应该完美地满足你的需求。


在我的GUI中,用户可以排列任何按钮的顺序和位置。用户还可以将按钮拖放到容器内或外。如果我创建一个表格布局面板,那么当用户将控件拖放到其中时,表格的行为并不是简单的,因为我们可以有一列控件、一行控件或者一个表格。 - Natalia
@Natalia:你是在尝试实现这个吗? - Victor Zakharov

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接