我假设 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 内置控件是 FlowLayoutPanel
和 TableLayoutPanel
;希望第三方组件也尊重它。其他场景基本没有影响。
关于c# - .NET Control.Margin 属性有什么用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75777/