.NET Control.Margin 属性有什么用?

发布于 2024-07-04 03:58:59 字数 234 浏览 7 评论 0原文

我假设 C# 的 margin 属性具有与 CSS 类似的含义 - 控件外部的间距。 但无论我输入什么值,边距值似乎都会被忽略。

然后我读到了SDK:

设置 Margin 属性 停靠的控件对 控制距离 其容器的边缘。

鉴于我将控件放置在表单上,​​并且可能对接它们,Margin 属性会给我带来什么?

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.

Then I read on the 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.

Given that I'm placing controls on forms, and perhaps docking them, what does the Margin property get me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

爱你是孤单的心事 2024-07-11 03:58:59

如果您不使用布局容器,而是手动放置控件,Control.Margin 属性在设计时也很有用。

它会影响手动拖动的控件之间的距离,对齐线出现。

例如,对于文本框的默认边距值为 3,您将具有以下对齐线:

在此处输入图像描述

对于边距 10 - 这些(在两种情况下标签的边距均为 3):

在此处输入图像描述

因此,如果您对 UI 有一些严格的指导原则,那么您只需根据需要设置边距并将控件拖动到对齐线即可。

Control.Margin property could be also useful at design time if you do not use layout container, but rather place controls manually.

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:

enter image description here

And for margin of 10 - these (label has margin of 3 in both cases):

enter image description here

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.

ゞ记忆︶ㄣ 2024-07-11 03:58:59

margin 属性由您的控制主机(例如,Panel)使用的任何布局引擎使用,以布局引擎认为合适的方式使用。 然而,正如您所假设的那样,它最好用于间距。 只需阅读该特定布局引擎的文档即可。

例如,当使用 FlowLayoutPanel 或 TableLayoutPanel 时,它可以非常方便 - 减少默认填充或稍微间隔一些。 显然,如果您编写自定义布局提供程序,则可以使用您认为合适的 Margin。

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.

活雷疯 2024-07-11 03:58:59

正如 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);
        }
    }
}

我相信唯一尊重此属性的 .NET 2.0 内置控件是 FlowLayoutPanelTableLayoutPanel; 希望第三方组件也尊重它。 其他场景基本没有影响。

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:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文