我有一个用户控件,其中包含一组控件,以便在UI中呈现数据时重用它们。我已经尝试实现“弹出”选项,将重新从父窗体上的另一个容器控件(一个面板,例如),创建一个新的标签页,然后将控件添加到标签页。如何修改已重新添加到TabPage的控件?
不幸的是,当控件添加到TabPage时,它的大小似乎被锁定到最后一个父对象的显示方式。
我覆盖了ParentChanged事件来检测控件何时实际添加到TabPage。如果我检查大小,请尝试将大小设置为TabPage的ClientRectangle,然后重新检查大小 - 它不会更改。设置Dock属性不会改变这种行为(尤其是填充)。
protected override void OnParentChanged(EventArgs e)
{
if (this.Parent != null)
{
Size oldSize = this.Size;
this.Size = this.Parent.ClientRectangle.Size;
if (this.Size == oldSize)
{
// this is where we end up
throw new Exception("We didn't change size!");
}
}
}