leviathanbadger
leviathanbadger

Reputation: 1712

Why does Control.Padding not affect the layout of a docked control?

When a control with Dock = DockStyle.Fill is a child of another control with any padding set, the docked control completely ignores the padding of the container control. This does not happen if the container control is a subclass of type UserControl.

For example, consider the following two classes.

[Designer("System.Windows.Forms.Design.ParentControlDesigner, System.Design", typeof(IDesigner))]
public class Container : Control
{
    public Container()
    {
        Dock = DockStyle.Fill;
        Padding = new Padding(30, 30, 30, 30);
        BackColor = Color.Blue;
        Controls.Add(new Contained());
    }
}

public class Contained : Control
{
    public Contained()
    {
        Dock = DockStyle.Fill;
        BackColor = Color.White;
    }
}

The DesignerAttribute is simply for use as an aid in the designer. Unless you change the Container control to inherit from UserControl, a Contained control will not conform to the Container control's Padding property.

Why is this? Do I have to use UserControl in order to dock another control in a custom control with a custom padding?

Note: I am running VS11 with .NET 4.5. This could be the problem, but I can't test it on another version or platform to find out for sure.

Thanks, Brandon

Upvotes: 1

Views: 2035

Answers (1)

Hans Passant
Hans Passant

Reputation: 941465

A container control should derive from the ContainerControl class. That gives it a number of "act like a container" behaviors. Including observing the Padding property.

Upvotes: 2

Related Questions