繁体   English   中英

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

[英]How can I resize the parent control from a child control's resize event?

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

例如。 假设一个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;
    }
}

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

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

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

}

暂无
暂无

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

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