System.Windows.Forms Namesp ...


.NET Framework Class Library
Padding Structure

Represents padding or margin information associated with a user interface (UI) element.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)
Syntax

Visual Basic (Declaration)
<SerializableAttribute> _
<TypeConverterAttribute(GetType(PaddingConverter))> _
Public Structure Padding
Visual Basic (Usage)
Dim instance As Padding
C#
[SerializableAttribute]
[TypeConverterAttribute(typeof(PaddingConverter))]
public struct Padding
Visual C++
[SerializableAttribute]
[TypeConverterAttribute(typeof(PaddingConverter))]
public value class Padding
JScript
JScript supports the use of structures, but not the declaration of new ones.
Remarks

The Padding structure represents the padding or margin associated with a rectangular UI element such as a control. The padding is the internal space between the body of the UI element and its edge. In contrast, a margin is the distance separating the adjoining edges of two adjacent UI elements. Because of structural similarities, Padding is used to represent both padding and margins.

For a diagram that illustrates the Padding and Margin properties on a control, see Margin and Padding in Windows Forms Controls.

Padding has a different effect on controls that are containers than on controls that are not. For example, in a Panel control, the Padding property defines the spacing between the border of the Panel and its child controls. For a Button control, the Padding property defines the spacing between the border of the Button control and its contained text.

In addition to typical methods and properties, Padding also defines the following type-level members:

  • The Empty field, which represents a predefined Padding with no padding.

  • A set of operators for performing common arithmetic operations for the class, such as adding two Padding objects together. For languages that do not support operator overloading, you can invoke these members by using alternative method syntax.

  • The Horizontal, Vertical, and Size properties, which provide combined values that are convenient for use in custom layout calculations.

Examples

The following code example demonstrates how to use the Padding property to create an outline around a RichTextBox control.

For a full code listing, see How to: Create a Border Around a Windows Forms Control Using Padding.

Visual Basic
' This code example demonstrates using the Padding property to 
' create a border around a RichTextBox control.
Public Sub New()
     InitializeComponent()

     Me.panel1.BackColor = System.Drawing.Color.Blue
     Me.panel1.Padding = New System.Windows.Forms.Padding(5)
     Me.panel1.Dock = System.Windows.Forms.DockStyle.Fill

     Me.richTextBox1.BorderStyle = System.Windows.Forms.BorderStyle.None
     Me.richTextBox1.Dock = System.Windows.Forms.DockStyle.Fill
 End Sub
C#
// This code example demonstrates using the Padding property to 
// create a border around a RichTextBox control.
public Form1()
{
    InitializeComponent();

    this.panel1.BackColor = System.Drawing.Color.Blue;
    this.panel1.Padding = new System.Windows.Forms.Padding(5);
    this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;

    this.richTextBox1.BorderStyle = System.Windows.Forms.BorderStyle.None;
    this.richTextBox1.Dock = System.Windows.Forms.DockStyle.Fill;
}
Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Platforms

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

Supported in: 3.5, 3.0, 2.0
See Also

Reference

Other Resources

Tags :


Community Content

Lost_In_JavaScript_Land
Directly setting Top, Bottom, Left and Right
This is just a note. You cannot directly set the properties of the Padding structure if referencing it from an object. For example, the following code in VB:

Dim lbl As New Label()
lbl.Padding.Bottom = 2

...will give you the error "Expression is a value and therefore cannot be the target of an assignment" when assigning the Bottom property. To set the value of Padding for an object, do something similar to this:

Dim lbl As New Label()
lbl.Padding = new Padding(0, 0, 0, 2)

Look at the reference for the Padding constructor for more information.
Tags :

Page view tracker