簡體   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