C# 如何从子控件调整父控件的大小';调整事件的大小?

C# 如何从子控件调整父控件的大小';调整事件的大小?,c#,.net,winforms,resize,C#,.net,Winforms,Resize,有没有什么好方法可以调整大小,例如当子控件(例如面板)大小更改时调整窗体高度 比如说。假设一个表单,里面有一个子面板。面板具有DockStyle.Fill。我们订阅panel_resize事件: private void panel_Resize(object sender, System.EventArgs e) { if (this.Width > 500) { //increment the size of the form this.

有没有什么好方法可以调整大小,例如当子控件(例如面板)大小更改时调整窗体高度

比如说。假设一个
表单
,里面有一个子面板。面板具有
DockStyle.Fill
。我们订阅panel_resize事件:

private void panel_Resize(object sender, System.EventArgs e)
{
    if (this.Width > 500)
    {
        //increment the size of the form
        this.Height += 100;
    }
    else
    {
        // decrement the size of the form
        this.Height -= 100;
    }
}

这种行为非常奇怪,因为我们试图在调整大小操作中调整窗体的大小。是否有其他方法来模拟此行为?

您可以通过尝试令牌设置绕过无限循环

// at your custom panel level...
private Boolean AmIResizing = false

private void panel_Resize(object sender, System.EventArgs e) 
{ 
    if(AmIResizing)
      return;

    // set flag so it immediately gets out after first time started
    AmIResizing = true;

    // do your other resizing
    // The settings here will otherwise force your recursive form level resizing calls
    // but with your up-front check on the flag will get out.
    // then, when the form is done with it's stuff...


    // Now, clear the flag
    AmIResizing = false;

}

这不是无限循环吗?您正在使用dock=fill调整面板的大小,因此表单会调整大小,但这会触发面板调整大小,依此类推?我想若面板不是dock=fill,那个么它是有意义的,否则它对我来说就并没有意义了。@Adrian:不是。如果在Form_resize事件中执行此操作,则将是无限循环。但在这种情况下,我不明白。如何将
面板
设置为
DockStyle.Fill
在不调整其父窗体大小的情况下调整其大小?你要求做的事情没有任何意义,不管是不是无限循环。