c# - .NET Control.Margin 属性有什么用?

标签 c# controls margin

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

然后我在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 属性对我有什么帮助?

最佳答案

正如 Philip Rieck 所说,margin 属性仅被执行布局的容器控件所尊重。下面是一个示例,可以清楚地说明 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;希望第三方组件也尊重它。其他场景基本没有影响。

关于c# - .NET Control.Margin 属性有什么用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75777/

相关文章:

java - 使用其图形在另一个内部绘制控件

Excel VBA 组合框禁用问题

css - 媒体查询 margin 失败

用于列表项之间分隔符的 CSS

c# - 如何拆分 ViewModel

c# - 如何让SqlConnection超时更快

c# - 注销并重定向到不同区域的主页

c# - 如何在 Windows 窗体中创建非阻塞控件?

c# - Cannot modify the logical children for this node at this time because a tree walk is in progress 是什么意思?

css - 垂直边距不重叠