【问题标题】:Create a ContainerControl with padding included创建一个包含填充的 ContainerControl
【发布时间】:2014-03-06 21:11:53
【问题描述】:
我想创建一个包含 25px Header 的控件。当我添加子控件时,我希望它们的行为就像在 GroupBox 中使用一样。
当控件的 DockStyle 设置为 Fill in a groupbox 时,它的位置自动设置为 3;16,其大小自动设置为 Width-6; Height-19。
我不想使用 Padding 或 Margin,因为它们似乎没有在 GroupBox 中使用。
如何在我自己的控件中实现相同的行为?
【问题讨论】:
标签:
c#
.net
winforms
controls
【解决方案1】:
尝试覆盖面板的 DisplayRectangle 属性:
public class MyContainer : Panel {
public override Rectangle DisplayRectangle {
get {
int headerHeight = 25;
return new Rectangle(
this.Padding.Left,
this.Padding.Top + headerHeight,
this.ClientSize.Width -
(this.Padding.Left + this.Padding.Right),
this.ClientSize.Height -
(this.Padding.Top + this.Padding.Bottom + headerHeight));
}
}
}