有没有办法在 C# WinForms 中自动调整宽度或自动调整控件的高度?

Is there a way to auto-width or auto-height a control in C# WinForms?

我有一个 FlowLayoutPanel,里面有一些按钮。我希望 FlowLayoutPanel 宽度与最大按钮相同,但高度固定。我怎样才能做到这一点?我用谷歌搜索了它,但我发现的都是关于 WPF 的。

您应该能够通过在控件上相应地设置锚点来实现这一点。 阅读 herehere.

您可以简单地通过控件进行 LINQ 并找到按钮的最大宽度:

int maxWidth = flp.Controls.OfType<Button>().Max(x => x.Width) + 
              (flp.Margin.Left + flp.Margin.Right);
flp.ClientSize = new Size(maxWidth, flp.ClientSize.Height);