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:

Padding

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 );

Share and Enjoy:
  • Digg
  • Twitter
  • Facebook
  • Reddit
  • StumbleUpon
  • LinkedIn
  • Google Bookmarks
  • Slashdot