使Windows Forms窗体可调整大小的最佳方法 [英] Best way to make Windows Forms forms resizable
问题描述
我正在使用大量Windows Forms表单进行大型C#项目开发,即使您可以调整表单的大小,但表单中的元素仍无法缩放。
I am working on a largish C# project with a lot of Windows Forms forms that, even though you can resize the form, the elements in the form don't scale.
当用户更改表单大小时,如何使表单元素(例如datagridview,文本区域等)缩放?
How can I make the form elements (such as the datagridview, text area's, etc.) scale when the user changes the size of the form?
几乎所有表单子类都来自一种特定的表单,因此,如果我可以在基类中做某些事情,那将是很棒的。
Nearly all the forms subclass from one specific form, so if there's something I can do in the base class, that'd be great.
推荐答案
您应在控件上设置 Anchor
和 Dock
属性
You should set the Anchor
and Dock
properties on the controls in the forms.
Anchor
属性控制控件的哪些边绑定或绑定到
例如,如果将 Anchor
设置为 Bottom
,
如果设置 Anchor $ c,则控件的底部边缘与其父级底部之间的距离不会改变,因此控件将在您调整窗体大小时向下移动。 $ c>至
顶部|底部
,控件将在调整表单大小时垂直调整大小。
The Anchor
property controls which edges of a control are "bound" or "tied" to the corresponding edges of its form.
For example, if you set Anchor
to Bottom
, the distance between the control's bottom edge and the bottom of its parent will not change, so the control will move down as you resize the form.
If you set Anchor
to Top | Bottom
, the control will resize vertically as you resize the form.
要使用表单调整控件大小,请设置在所有四个侧面都固定,或者将
停靠 c设置为
填充
。
To make a control resize with the form, set the Anchor
to all four sides, or set Dock
to Fill
.
这篇关于使Windows Forms窗体可调整大小的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!