你如何修改已经被重新父到TabPage的控制?TabPage

2023-09-06 06:51:55 作者:与风相拥

我有一个包含被重复使用的UI presenting数据控件集合的用户控件。我一直试图推行的弹出式选项,将重新从父窗体上的另一个容器中的控件(面板,例如),创建一个新的标签页,然后将控件添加到标签页。

不幸的是,当控制被添加到TabPage的,其大小似乎被锁定到它被presented与上次母体的方式。

我推翻了ParentChanged事件来检测时,控制实际添加到的TabPage。如果我检查的大小,尝试将大小设置为的TabPage的ClientRectangle,然后重新审视的大小 - 它不会改变。设置Dock属性不会改变这种行为(特别是填写)。

 保护覆盖无效OnParentChanged(EventArgs的发送)
{
    如果(this.Parent!= NULL)
    {
        大小oldSize = this.Size;

        this.Size = this.Parent.ClientRectangle.Size;

        如果(this.Size == oldSize)
        {
            //这是我们结束了
            抛出新的异常(我们并没有改变大小!);
        }
    }
}
 

解决方案

对照第一次加入到一个容器控件。跳水进入容器code的肠子,通过反射,它被设置在最小/最大尺寸值,我控制了正在重设父。我曾以为,不准确,默认最小/最大设置保持从设计师的默认值。容器控件遗憾的是决定不同。走,走,反射!这种行为,当然,没有文档。

感谢大家的帮助。

c 2010tabControl1里tabpage属性里杂项是如何设置出来的,添加的新tabControl为什么没有这项设置

I have a user control that contains a collection of controls to be reused for presenting data on the UI. I've attempted implementing a "pop-out" option that will re-parent the control from another container on the form (a Panel, for example), create a new tab page, and then add the control to the tab page.

Unfortunately, when the control is added to the TabPage, its size appears to be locked to the way it was presented with the last parent.

I overrode the ParentChanged event to detect when the control was actually added to the TabPage. If I examine the size, attempt to set the size to the TabPage's ClientRectangle, and then re-examine the size - it does not change. Setting the Dock property does not alter this behavior (especially Fill).

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!");
        }
    }
}

解决方案

The control was first added to a container control. Diving into the bowels of the Container code, via Reflector, it was setting the min/max size values for my control that was being reparented. I had assumed, inaccurately, that the default min/max settings were left as the default from the designer. The container control unfortunately decided differently. Go, go, Reflector! This behavior was, of course, not documented.

Thank you all for your help.