C# .NET Control.Margin 属性有什么用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/75777/
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
What is the .NET Control.Margin property for?
提问by Ian Boyd
I assumed that the C# margin property had a meaning like in CSS - the spacing around the outside of the control. But Margin values seem to be ignored to matter what values I enter.
我认为 C# 的边距属性在 CSS 中具有类似的含义 - 控件外部周围的间距。但是对于我输入的值,保证金值似乎被忽略了。
Then I read on the SDK:
然后我在 SDK 上阅读:
Setting the Margin property on a docked control has no effect on the distance of the control from the the edges of its container.
在停靠控件上设置 Margin 属性不会影响控件与其容器边缘的距离。
Given that I'm placing controls on forms, and perhaps docking them, what does the Margin property get me?
鉴于我在窗体上放置控件,并且可能停靠它们,Margin 属性对我有什么作用?
采纳答案by Philip Rieck
The margin property is used by whatever layout engine your control host (Panel, for example) is using, in whatever way that layout engine sees fit. However, it is best used for spacing just as you assume. Just read the documentation for that specific layout engine.
无论您的控件主机(例如面板)正在使用的任何布局引擎,都以该布局引擎认为合适的任何方式使用边距属性。但是,正如您所假设的那样,它最好用于间距。只需阅读该特定布局引擎的文档即可。
It can be very handy when using a FlowLayoutPanel or TableLayoutPanel, for example - to either reduce the default padding or space things out a bit. Obviously, if you write a custom layout provider, you can use Margin however you see fit.
例如,当使用 FlowLayoutPanel 或 TableLayoutPanel 时,它可能非常方便 - 减少默认填充或空间一些东西。显然,如果您编写自定义布局提供程序,则可以使用您认为合适的 Margin。
回答by OwenP
Like Philip Rieck said, the margin property is only respected by container controls that perform layout. Here's an example that makes it fairly clear how the TableLayoutPanel
respects the Margin property:
就像 Philip Rieck 所说的那样,只有执行布局的容器控件才会尊重边距属性。下面是一个示例,它相当清楚地说明了如何TableLayoutPanel
尊重 Margin 属性:
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
TableLayoutPanel pnl = new TableLayoutPanel();
pnl.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 50));
pnl.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 50));
pnl.Dock = DockStyle.Fill;
this.Controls.Add(pnl);
Button btn1 = new Button();
btn1.Text = "No margin";
btn1.Dock = DockStyle.Fill;
Button btn2 = new Button();
btn2.Margin = new Padding(25);
btn2.Text = "Margin";
btn2.Dock = DockStyle.Fill;
pnl.Controls.Add(btn1, 0, 0);
pnl.Controls.Add(btn2, 1, 0);
}
}
}
I believe the only .NET 2.0 built-in controls that respect this property are FlowLayoutPanel
and TableLayoutPanel
; hopefully third-party components respect it as well. It has basically no effect in other scenarios.
我相信唯一尊重此属性的 .NET 2.0 内置控件是FlowLayoutPanel
和TableLayoutPanel
;希望第三方组件也尊重它。在其他场景下基本没有效果。
回答by Pavel K
Control.Margin property could be also useful at design time if you do not use layout container, but rather place controls manually.
如果您不使用布局容器而是手动放置控件,则 Control.Margin 属性在设计时也很有用。
It affects the distance between manually dragged controls at which snaplines appear.
它会影响对齐线出现的手动拖动控件之间的距离。
E.g. for default margin value of 3 for text box you would have this snaplines:
例如,对于文本框的默认边距值为 3,您将拥有以下对齐线:
And for margin of 10 - these (label has margin of 3 in both cases):
对于 10 的边距 - 这些(标签在两种情况下的边距均为 3):
So if you have some strict guidelines for you UI then you just set the margins as you need and drag controls to the snaplines.
因此,如果您对 UI 有一些严格的指导,那么您只需根据需要设置边距并将控件拖动到对齐线。