在 Windows Forms 控件中调整大小以及随着窗体的调整而调整大小

4
我在C# Windows表单应用程序中有一些控件(如group boxes、tables、gridview等),我希望根据屏幕宽度/高度来进行缩放。
例如,屏幕从640x480开始,然后最大化到1920x1200。
我想要能够增加控件的宽度/高度,以便在窗口重新调整大小后它们看起来完全相同。
在不手动设置每个宽度/高度属性的情况下,最好的方法是什么?
2个回答

10
没有手动设置每个宽度/高度属性的最佳方法是什么?
可以使用 AnchorDock 属性,使控件根据其包含元素进行缩放,而不是指定宽度和高度。
或者,您可以使用 TableLayoutPanelFlowLayoutPanel 来排列您的控件。

这很有道理,我已经在尝试了一些东西,但我的设计是非标准的,Dock 只提供顶部、底部和 3 个中心项。我能否以某种方式创建自定义 Dock? - sd_dracula
@sd_dracula 你不能自定义 - 你需要按照标准布局思考。如果你正在使用“网格”样式,TableLayoutPanel可能是一个更容易的方法。 - Reed Copsey

0

我相信你想要控件的 Anchor 属性。

As the name implies, this property forces the control to anchor itself in a relative or absolute position within the parent form or control. This property has four values that can be turned on or off:

Top -- Indicates that the control should keep its top edge stationary in respect to the parent form (or control) top edge.
Bottom -- Indicates that the control should keep its bottom edge stationary in respect to the parent form (or control) bottom edge.
Left -- Indicates that the control should keep its left edge stationary in respect to the parent form (or control) left edge.
Right -- Indicates that the control should keep its right edge stationary in respect to the parent form (or control) right edge.

如果我没记错的话,您想使用Ctrl+A选择表单上的所有控件,然后将锚点属性设置为顶部、底部、左侧和右侧。


对齐它们没问题,但我希望它们根据表单的新宽度/高度来扩展宽度/高度。这样可以吗? - sd_dracula

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