如何从子控件的resize事件调整父控件的大小?

时间:2011-02-15 11:18:21

标签: c# .net winforms resize

有没有什么好方法可以调整大小,例如当子控件(例如面板)大小发生变化时的表单高度?

例如。假设一个Form里面有一个子面板。该小组有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;
    }
}

这种行为很奇怪,因为我们试图在调整大小操作中调整表单大小。有没有其他方法可以模拟这种行为?

1 个答案:

答案 0 :(得分:0)

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

// 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;

}