简体   繁体   English

如何在Winforms的复合控件中更改子控件的大小?

[英]How to change the size of the child controls in a composite control in winforms?

I basically created 2 child controls and I want to set their width to be equal to the width of the parent composite control. 我基本上创建了2个子控件,我想将其宽度设置为等于父组合控件的宽度。 The problem is when I do this using the parent.Size property, it doesn't work. 问题是当我使用parent.Size属性执行此操作时,它不起作用。 It only works once when you add the control. 当您添加控件时,它只能工作一次。

Am I supposed to override an event not a property? 我应该覆盖事件而不是属性吗? I thought the property change would be signaled with a resize, right? 我认为属性更改会通过调整大小来表示,对吗?

EDIT: 编辑:

Here is the code, property doesn't work, but the OnResize event worked. 这是代码,属性不起作用,但是OnResize事件起作用。

Q2: Shouldn't OnResize EventArgs e give me the new size? 问题2:OnResize EventArgs不应该给我新的尺寸吗?

public partial class CollapsableCtrl : UserControl
{
    public CollapsableCtrl ( )
    {
        this.ChildCtrl = new CustomCtrl ( );
        this.Size = new Size ( 181, 82 );
        this.Controls.Add ( this.ChildCtrl );
    }

    CustomCtrl ChildCtrl { get; set; }

    public new Size Size
    {
        get { return base.Size; }
        set
        {
            this.ChildCtrl.Size = value;
            Invalidate ( );
        }
    }

    protected override void OnResize ( EventArgs e )
    {
        base.OnResize ( e );
        this.ChildCtrl.Size = this.Size;
    }
}

Do you have properties like Dock or Anchor set for your child controls in a way that would prevent them from being resized to arbitrary dimensions? 您是否为子控件设置了Dock或Anchor之类的属性,以防止将其调整为任意尺寸? I do this all of the time and simply setting a child control's Size, Width, or Height property has always worked for me. 我一直都这样做,只是设置子控件的Size,Width或Height属性一直对我有用。 If my guess is wrong, it would help us to see your code. 如果我的猜测是错误的,它将帮助我们查看您的代码。

EDIT: After seeing your comment, I think that you should be using Dock and/or Anchor. 编辑:看到您的评论后,我认为您应该使用Dock和/或Anchor。 This way, you can lay out the child controls first and then, when the parent is resized, the child controls will follow suit with no extra work on your end. 这样,您可以先对子控件进行布局,然后在调整父控件的大小时,子控件将随之效仿,而无需您进行额外的工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM