The .NET 2.0 upgrade included many minor improvements that are easily overlooked. One such improvement is Padding, a handy structure in the System.Windows.Forms namespace that you may find useful for representing offsets, margins or padding in the user interface.
Whereas the Rectangle structure has Left/Top/Width/Height as its primary properties, the Padding structure has Left/Top/Right/Bottom. For example, you can use the Padding structure to represent the offset between a UI element’s border and its contents, shown as blue in the image below:
This example could be represented by the following code:
Padding pad = new Padding( 5, 3, 1, 2 );
The Padding structure also has Horizontal and Vertical properties. The Horizontal property sums the Left and Right padding; Vertical sums Top and Bottom. The All property sums all four values.
Note that if all four values are the same, you can use a constructor that takes a single value:
Padding pad = new Padding( 2 );
Do you know if there is something else you have to set up to get padding to work? If I start with a blank form and add a panel and set that panel’s padding to 10, and then add a button to that panel, the button doesn’t seem to be padded at all.
Padding doesn’t work if you manually position the button on the panel. However, if you set the button’s Dock property to Fill, for example, you will notice the button honors the panel’s Padding value.
^thanks for that. This page is the only google result explaining why padding gets ignored.