C# 自动调整 Windows 窗体控件的大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1636280/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 19:34:52  来源:igfitidea点击:

Automatic resizing of the Windows Forms controls

c#.netwinforms

提问by claws

I'm using VS2008's designer for doing this.

我正在使用 VS2008 的设计器来做这件事。

For example if I've a windows form of size say 500x500 and I added a DataGridView to it (490x490).

例如,如果我有一个大小为 500x500 的 Windows 窗体,并且我向它添加了一个 DataGridView (490x490)。

when I run this program. and maximize the form, the DataGridView still remains of the same size. rest of the additional space is blank on the form. I want DataGridView to also take the entire window size.

当我运行这个程序时。并最大化表单,DataGridView 仍然保持相同的大小。其余的额外空间在表单上是空白的。我希望 DataGridView 也采用整个窗口大小。

No software will be like that. I don't know what to change inorder get desired behaviour.

没有软件会这样。我不知道要更改什么才能获得所需的行为。

采纳答案by sindre j

You can use Dock, or for more precise control use the Anchor property. By setting Anchor to Left, Right, Top, Bottom the control will scale with the window. By setting Anchor to Right the control will move with the right border. By setting Anchor to Top and Bottom the control will scale vertically, but have a fixed width. Just experiment

您可以使用 Dock,或者使用 Anchor 属性进行更精确的控制。通过将 Anchor 设置为 Left、Right、Top、Bottom,控件将随窗口缩放。通过将 Anchor 设置为 Right,控件将随右边框移动。通过将 Anchor 设置为 Top 和 Bottom,控件将垂直缩放,但具有固定的宽度。只是实验

回答by Andrew Shepherd

Set the Dockproperty of the DataGridViewto Fill.

DataGridViewDock属性设置为 Fill。

回答by Frank Bollack

You can ether set the Dock-Property of your DataGridView instance to DockStyle.Fillor use the Anchor-Property and set the anchors to:

您可以Dock将 DataGridView 实例DockStyle.FillAnchor-Property 设置为或使用-Property 并将锚点设置为:

dataGridView.Anchor = 
    AnchorStyles.Bottom | 
    AnchorStyles.Right | 
    AnchorStyles.Top | 
    AnchorStyles.Left;

The first method will make your DataGridView to fill your whole client area. The second method will keep the ratio and only resize the control if the container resizes.

第一种方法将使您的 DataGridView 填充您的整个客户区。第二种方法将保持比例,并且仅在容器调整大小时调整控件大小。

回答by Webking

for filled up docking inside the winform use:

用于在 winform 内填充对接使用:

dataGridView.Dock = DockStyle.Fill;

回答by Irfan Babar

If you need other things like textbox , picturebox etc as auto scaling then go to their relevant anchor property and set is according to your desire result.

如果您需要其他东西,如 textbox 、picturebox 等作为自动缩放,请转到它们的相关锚点属性并根据您的期望结果进行设置。