C# 在 WinForms 中调整单个控件的大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/770094/
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
Resizing a Single Control In WinForms
提问by THE DOCTOR
How might I design a UI in C#/WinForms which happens to contain several different control types such that only the ListView control gets resized if the user resizes the window?
我如何在 C#/WinForms 中设计一个 UI,它恰好包含几种不同的控件类型,这样如果用户调整窗口大小,只有 ListView 控件会被调整大小?
采纳答案by Simon Gillbee
There are two primary ways to make a control automatically resize based on size changes of the parent container (a Form in your case):
有两种主要方法可以使控件根据父容器(在您的情况下为 Form)的大小更改自动调整大小:
- Set the Dock property of the control to DockStyle.Fill.
- Set the Anchor property to "Top, Bottom, Left, Right"
- 将控件的 Dock 属性设置为 DockStyle.Fill。
- 将锚属性设置为“上、下、左、右”
Use the Dock property with Dock.Fill
将 Dock 属性与 Dock.Fill 结合使用
The advantage of this method is that it takes the entire control and tells it to always fill the entire client area of the parent container (in your case, the Form client area). That's useful if you want to do something like fill a Form with a ListControl or TreeView or something like that. But it's not as useful if you want to scale a single control while using other controls (as you indicate is your need). In that case, you would need to set the Dock property on those other controls to DockStyle.Top or DockStyle.Bottom to have them float above or below your main resizing control.
这种方法的优点是它获取整个控件并告诉它始终填充父容器的整个客户区(在您的情况下,是窗体客户区)。如果你想做一些事情,比如用 ListControl 或 TreeView 或类似的东西填充表单,这很有用。但是,如果您想在使用其他控件的同时缩放单个控件(如您所指的是您的需要),它就没有那么有用了。在这种情况下,您需要将这些其他控件上的 Dock 属性设置为 DockStyle.Top 或 DockStyle.Bottom,以使它们浮动在主调整大小控件的上方或下方。
That's a hassle and it also limits the layout options of the other controls. You can mitigate that problem by docking two Panel controls, one at the top and another at the bottom of the Form. Those panels will remain in fixed positions while the middle area (with your DockStyle.Fill control) scales with the parent Form. You can then put any controls in any layout configuration in those "header" and "footer" panels.
这很麻烦,而且还限制了其他控件的布局选项。您可以通过停靠两个 Panel 控件来缓解该问题,一个在窗体的顶部,另一个在窗体的底部。这些面板将保持在固定位置,而中间区域(使用 DockStyle.Fill 控件)随父窗体缩放。然后,您可以在这些“页眉”和“页脚”面板中的任何布局配置中放置任何控件。
This kind of composite form-building using docked panels is incredibly powerful. Quite frankly, it was game changing in .NET when they introduced this with .NET 1.0 WinForms.
这种使用对接面板的复合形式构建非常强大。坦率地说,当他们在 .NET 1.0 WinForms 中引入它时,它改变了 .NET 的游戏规则。
Use the Anchor property with "Top, Bottom, Left, Right"
将 Anchor 属性与“顶部、底部、左侧、右侧”一起使用
If all you want to do is have a single control on a form scale, while others stay "stuck" to the edges, use the Anchor property. For the controls that you want to stay at the top, set the Anchor property to "Top, Left" (the default). For controls that you want to stay at the bottom, set the Anchor property to "Bottom, Left". For controls that you want to grow in width with the form/dialog (such as a single-line textbox control), set the Anchor property to "Left, Right" (and set Top or Bottom depending whether you want it move as the top or the bottom of the dialog changes.
如果您想要做的只是在表单比例上有一个控件,而其他控件则“卡在”边缘,请使用 Anchor 属性。对于要保持在顶部的控件,将 Anchor 属性设置为“顶部,左侧”(默认值)。对于要留在底部的控件,请将 Anchor 属性设置为“底部,左侧”。对于您希望通过表单/对话框(例如单行文本框控件)增加宽度的控件,将 Anchor 属性设置为“左、右”(并根据您是否希望它作为顶部移动来设置顶部或底部或对话框底部发生变化。
And if you want a control to resize in all directions with a Form, set the Anchor property to "Top, Left, Bottom, Right". Very useful for "main control" type of things, such as a dominant listbox, tree control, or multi-line textbox.
如果您希望控件在所有方向上使用 Form 调整大小,请将 Anchor 属性设置为“上、左、下、右”。对于“主控件”类型的东西非常有用,例如占主导地位的列表框、树控件或多行文本框。
For what you need, don't mess with the AutoSize or AutoSizeMode... those properties control how a control changes size based on its own contents, not how it resizes based on its container's behavior. Think of AutoSize/AutoSize mode as inward looking, while Anchor/Dock are outward looking. You get some very bizarre behavior if you use both sizing methods at the same time. Generally not useful.
对于您所需要的,不要与 AutoSize 或 AutoSizeMode 混为一谈……这些属性控制控件如何根据其自身内容更改大小,而不是如何根据其容器的行为调整大小。将 AutoSize/AutoSize 模式视为向内看,而 Anchor/Dock 则是向外看。如果您同时使用两种大小调整方法,您会得到一些非常奇怪的行为。一般用处不大。
回答by Shea
IF you put the ListView in one panel of a SplitContainer and put the remaining controls in the other panel you can restrict the growth of the second panel by setting the min and maxsize.
如果将 ListView 放在 SplitContainer 的一个面板中并将其余控件放在另一个面板中,则可以通过设置 min 和 maxsize 来限制第二个面板的增长。
If your ListView is docked Full then it'll take all the increase when the form is resized.
如果您的 ListView 停靠在 Full 中,那么在调整表单大小时它将占用所有增量。
回答by Inisheer
Dock the ListView to all four sides of the form, and the other controls to 2 or less.
将 ListView 停靠到窗体的所有四个边,将其他控件停靠到 2 个或更少。
回答by THX-1138
There is a property on controls called "Anchor" (in "Layout" category) if you set this to "Top, Bottom, Left, Right" it will maintain margins between control and its parent container causing it to resize as container changes size.
控件上有一个名为“Anchor”(在“布局”类别中)的属性,如果您将其设置为“顶部、底部、左侧、右侧”,它将保持控件与其父容器之间的边距,从而在容器更改大小时调整其大小。
But if only one of anchors along one axis is enabled (e.g. "left", but not "right") it will move the control instead, again, preserving locked margins between the control and its container.
但是,如果仅启用沿一个轴的锚点之一(例如“左”,而不是“右”),它将移动控件,同样,保留控件与其容器之间的锁定边距。
In short : exactly what James said, except it is "Anchor" not "Dock" property. Dock is similar but not exactly the same.
简而言之:正是詹姆斯所说的,除了它是“锚点”而不是“停靠点”属性。Dock 类似但不完全相同。
回答by Tathagat Verma
What if we have multiple controls in the form?
如果表单中有多个控件怎么办?
For example: If a form is used to generate some result in a grid with respect to the data entered in couple of text-boxes or combo-boxes, etc.;
And we want them to resize/realign accordingly and not overlap each other (as it happens when using the dock-fill), especially with the grid-view or similar control in context.
例如:如果一个表单用于在一个网格中生成一些关于在几个文本框或组合框等中输入的数据的结果;并且我们希望它们相应地调整大小/重新对齐并且不相互重叠(就像使用dock-fill 时发生的那样),尤其是使用grid-view 或上下文中的类似控件。