如何修改已重新设置 TabPage 父级的控件?

发布于 2024-08-25 00:48:45 字数 667 浏览 14 评论 0原文

我有一个用户控件,其中包含要重用的控件集合,用于在 UI 上呈现数据。我尝试实现一个“弹出”选项,该选项将从表单上的另一个容器(例如面板)重新设置控件的父级,创建一个新的选项卡页,然后将控件添加到选项卡页。

不幸的是,当控件添加到 TabPage 时,其大小似乎被锁定为最后一个父级的呈现方式。

我重写了 ParentChanged 事件来检测控件何时实际添加到 TabPage。如果我检查大小,尝试将大小设置为 TabPage 的 ClientRectangle,然后重新检查大小 - 它不会改变。设置 Dock 属性不会改变此行为(尤其是 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!");
        }
    }
}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

深海不蓝 2024-09-01 00:48:45

我怀疑,如果您已将控件设置为 Dock,那么它可能会忽略更改其大小/位置的尝试。就我个人而言,我会关闭 Dock/Anchor/AutoSize 行为,然后手动设置控件的位置和大小。我做过很多这种类型的事情,从来没有遇到过任何问题,所以我怀疑你只是在与自动布局选项作斗争,而且它正在获胜。

如果您希望 Dock 为您完成这项工作,您可能需要踢掉选项卡以启动其布局的重新计算? (有一个 Control.LayoutEngine 对象可能能够做到这一点,但我只是在这里假设一个可能的调查途径 - 我自己还没有尝试过)

无论如何,我希望这可以给你一些想法......

I suspect that if you have set the control to Dock then it probably will ignore attempts to change its size/position. Personally I'd turn off Dock/Anchor/AutoSize behaviour and just set the control's position and size manually. I've done this type of thing a lot and never had any problems, so I suspect you're just fighting an auto-layout option, and it's winning.

If you want Dock to do the work for you, you may need to kick the tab to initiate a recalculation of its layout? (there is a Control.LayoutEngine object that may be able to do this, but I'm just postulating a possible avenue of investigation here - I haven't tried it myself)

I hope that might give you some ideas, anyway...

神经大条 2024-09-01 00:48:45

该控件首先被添加到容器控件中。通过 Reflector 深入了解容器代码的内部,它正在为正在重新设置父级的控件设置最小/最大大小值。我错误地认为默认的最小/最大设置被保留为设计者的默认设置。不幸的是,集装箱控制部门做出了不同的决定。走吧,走吧,反射镜!当然,这种行为没有记录在案。

谢谢大家的帮助。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文