, (: )... . MS, :
FlowLayoutPanel . , .
. FlowLayoutPanel , , .
, Dock/Anchor / . FlowLayoutPanel , , , , , Dock Anchor " ". , , , .
, ? 0 FlowLayoutPanel, . false. Resize/Layout FlowLayoutPanel, . . , , , .
, " " . , "" , , , . , FlowLayoutPanel , , , , FlowLayoutPanel, . , , , , IDE. , , ControlDesigner. .
, FlowLayoutPanel, ClientSize . , , , . , , , FlowLayoutPanel.
, , , ; -)
using System;
using System.Collections;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing;
using System.Windows.Forms;
namespace ControlTest
{
public sealed class InvisibleControl : Control
{
public InvisibleControl()
{
TabStop = false;
}
#region public interface
public new AnchorStyles Anchor
{
get { return base.Anchor; }
set { base.Anchor = AnchorStyles.None; }
}
public new DockStyle Dock
{
get { return base.Dock; }
set { base.Dock = DockStyle.None; }
}
public new Point Location
{
get { return base.Location; }
set { base.Location = Point.Empty; }
}
private Orientation _orientation = Orientation.Horizontal;
[DefaultValue(typeof(Orientation), "Horizontal")]
public Orientation Orientation
{
get { return _orientation; }
set
{
if (_orientation == value) return;
_orientation = value;
ChangeSize();
}
}
#endregion
#region overrides of default behaviour
protected override Padding DefaultMargin => Padding.Empty;
protected override void Dispose(bool disposing)
{
if (disposing)
SetParent(null);
base.Dispose(disposing);
}
protected override void OnHandleCreated(EventArgs e)
{
base.OnHandleCreated(e);
ChangeSize();
}
protected override void OnPaint(PaintEventArgs e) { }
protected override void OnPaintBackground(PaintEventArgs pevent) { }
protected override void OnParentChanged(EventArgs e)
{
base.OnParentChanged(e);
SetParent(Parent);
ChangeSize();
}
protected override void OnResize(EventArgs e)
{
base.OnResize(e);
ChangeSize();
}
#endregion
#region private stuff
private void ChangeSize(object sender = null, EventArgs e = null)
{
Rectangle client = Parent?.ClientRectangle ?? new Rectangle(0, 0, 10, 10);
Size proposedSize = _orientation == Orientation.Horizontal
? new Size(client.Width, 0)
: new Size(0, client.Height);
if (!Size.Equals(proposedSize)) Size = proposedSize;
}
private Control boundParent;
private void SetParent(Control parent)
{
if (boundParent != null)
boundParent.Resize -= ChangeSize;
boundParent = parent;
if (boundParent != null)
boundParent.Resize += ChangeSize;
}
#endregion
}
}