I’m trying to resize a custom panel control so that it fits in the parent container width-wise. When I check the controls values they are both the same, yet the child control is too wide for the parent control.
What is causing the difference? I want to be able to calculate the correct size. I tried changing padding and margin options but this didn’t have any effect.

[Category("Collapsible Panel")]
[DesignOnly(true)]
[DefaultValue(false)]
[Description("If True, fits the panel to match the parent width")]
public bool FitToParent
{
get { return _fitToParent; }
set {
_fitToParent = value;
if (_fitToParent)
{
if (this.Parent != null)
{
this.Location = new Point(0, this.Location.Y);
this.Size = new Size(this.Parent.Size.Width, this.Size.Height);
this.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
}
}
else
{
this.Anchor = AnchorStyles.Top | AnchorStyles.Left;
}
}
}
Work with the
ClientSizeof the forms and controls.form.Widthorcontrol.Widthis the outer width including the borders. TheClientRectangleis what is between the borders where other controls can be placed andClientSizeis the size of this inner rectangle.