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?
You use margin control to control margins for sales orders and quotations that are created for standard items. If the net price of the sales quotation or order exceeds the defined margins, the appropriate action is performed. For example, if the margin of an order is exceeded, the order is blocked.
The TableLayoutPanel control arranges its contents in a grid. Because the layout is done both at design time and run time, it can change dynamically as the application environment changes.
The Add, Remove, and RemoveAt methods enable you to add and remove individual controls from the collection. You can also use the AddRange or Clear methods to add or remove all the controls from the collection. You can determine if a Control is a member of the collection by passing the control into the Contains method.
Property Value The amount of space between the content of a Control and its Margin or Border.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With