是否可以使FlowLayoutPanel中插入的项目自动调整FlowLayoutPanel的大小?下面是一个示例:
一个包含1个FlowLayoutPanel和3个按钮的表单:

如果我调整窗体的大小,控件看起来像这样:它们“从左到右”排列。

我想要的是:控件应该具有FlowLayoutPanel的宽度:

你知道该怎么做吗?我更改了FlowDirection并使用了Anchor属性,但没有成功。
我当然可以在FlowLayoutPanel_Resize事件中调整控件的大小,但我想添加大约500个用户控件-我测试了它,它很慢。
发布于 2011-03-23 04:40:23
在这种情况下,我建议您使用具有一个列的TableLayoutPanel。我发现TableLayoutPanel比FlowLayoutPanel更可预测、更可靠。
如果您仍然想使用FlowLayoutPanel,另一个选择是将第一个控件宽度设置为所需的宽度,然后对所有其他控件使用Dock = Top。
发布于 2013-03-21 00:27:45
这是一种简单的方法。只需绑定flowLayoutPannel的SizeChanged事件并调整包含控件的大小即可。像这样:
private void myFlowLayoutPannel_SizeChanged(object sender, EventArgs e)
{
myFlowLayoutPannel.SuspendLayout();
foreach (Control ctrl in pnSMS.Controls)
{
if (ctrl is Button) ctrl.Width = pnSMS.ClientSize.Width;
}
myFlowLayoutPannel.ResumeLayout();
}发布于 2017-04-12 16:16:57
下面是我的StackPanel类:
/// <summary>
/// A stackpanel similar to the Wpf stackpanel.
/// </summary>
public class StackPanel: FlowLayoutPanel
{
public StackPanel(): base()
{
InitializeComponent();
this.ForceAutoresizeOfControls = true;
}
private void InitializeComponent()
{
this.SuspendLayout();
//
// StackPanel
//
this.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
this.WrapContents = false;
this.ResumeLayout(false);
}
/// <summary>
/// Override it just in order to hide it in design mode.
/// </summary>
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public new bool WrapContents
{
get { return base.WrapContents; }
set { base.WrapContents = value; }
}
/// <summary>
/// Override it just in order to set its default value.
/// </summary>
[DefaultValue(typeof(AutoSizeMode), "GrowAndShrink")]
public override AutoSizeMode AutoSizeMode
{
get { return base.AutoSizeMode; }
set { base.AutoSizeMode = value; }
}
/// <summary>
/// Get or set a value that when is true forces the resizing of each control.
/// If this value is false then only control that have AutoSize == true will be resized to
/// fit the client size of this container.
/// </summary>
[DefaultValue(true)]
public bool ForceAutoresizeOfControls { get; set; }
protected override void OnSizeChanged(EventArgs e)
{
base.OnSizeChanged(e);
this.SuspendLayout();
switch (FlowDirection)
{
case FlowDirection.BottomUp:
case FlowDirection.TopDown:
foreach (Control control in this.Controls)
if (ForceAutoresizeOfControls || control.AutoSize)
control.Width = this.ClientSize.Width - control.Margin.Left - control.Margin.Right;
break;
case FlowDirection.LeftToRight:
case FlowDirection.RightToLeft:
foreach (Control control in this.Controls)
if (ForceAutoresizeOfControls || control.AutoSize)
control.Height = this.ClientSize.Height - control.Margin.Top - control.Margin.Bottom;
break;
default:
break;
}
this.ResumeLayout();
}
protected override void OnLayout(LayoutEventArgs levent)
{
base.OnLayout(levent);
if (levent != null && levent.AffectedControl != null)
{
Control control = levent.AffectedControl;
if (ForceAutoresizeOfControls || control.AutoSize)
{
switch (FlowDirection)
{
case FlowDirection.BottomUp:
case FlowDirection.TopDown:
control.Width = this.ClientSize.Width - control.Margin.Left - control.Margin.Right;
break;
case FlowDirection.LeftToRight:
case FlowDirection.RightToLeft:
control.Height = this.ClientSize.Height - control.Margin.Top - control.Margin.Bottom;
break;
default:
break;
}
}
}
}
}https://stackoverflow.com/questions/5395754
复制相似问题