调整单个控制在的WinForms [英] Resizing a Single Control In WinForms

查看:159
本文介绍了调整单个控制在的WinForms的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么可能设计在C#/的WinForms一个UI恰好包含几种不同的控制类型,例如,如果用户调整窗口大小只有ListView控件被调整?

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?

推荐答案

有两种主要方式,以自动调整基于父容器大小的变化(在你的情况表单)的控制:

There are two primary ways to make a control automatically resize based on size changes of the parent container (a Form in your case):


  1. 设置控件DockStyle.Fill的Dock属性。

  2. 设置Anchor属性上,下,左,右

该方法的优点是,它需要整个控制并告诉它总是充满父容器的整个客户端区域(在你的情况,表单客户区)。如果你想要做的事就像填表用列表控件或TreeView控件或类似的东西是非常有用的。但是如果你想同时使用其他控件扩展一个单一的控制(如你指明是你需要的),它不是那么有用。在这种情况下,你就需要设置其他控件DockStyle.Top或DockStyle.Bottom Dock属性让它们漂浮高于或低于你的主要调整大小的控制。

Use the Dock property with 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.

这是一个麻烦,这也限制了其他控件的布局选项。您可以通过对接两个Panel控件,一个在顶部,另一个在底部形态缓解这一问题。这些面板将保持在固定的位置,而中间区域(与你的DockStyle.Fill控制)与父窗体缩放。然后,你可以把任何控件中的任何布局配置在这些头和尾面板。

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.

这种复合形式建设使用停靠面板是令人难以置信的强大。坦率地说,这是游戏.NET改变时,他们推出的这款使用.NET 1.0的WinForms。

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.

如果你想要做的是有一个形成规模的单一控制,而其他留卡壳的边缘,使用Anchor属性。对于你想留在顶部的控件,设置Anchor属性上,左(默认值)。对于你想留在底部控件,设置Anchor属性下,左。对于要在表单/对话框(如单行文本框控件)宽度增长的控制,设置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属性上,左,下,右。对于主控式的东西,如一个列表框主导,树形控件,或者多行文本框非常有用的。

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.

有关你所需要的,不要乱用自动调整大小或AutoSizeMode ......这些属性控制如何根据自己的内容的控制变更的大小,而不是它如何调整大小基于其容器的行为。自动调整大小/自动调整大小模式看作内向型,而锚/码头向外看。如果你同时使用这两种方法的浆纱你得到一些非常怪异的行为。一般没有用的。

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.

这篇关于调整单个控制在的WinForms的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆