开发者

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

Is there any good way to resize, for example the form height when a child control (for example 开发者_JAVA百科a panel) size changes?

For example. Suppose a Form with a child panel inside. The panel has DockStyle.Fill. We subscribe to the panel_resize event:

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

The behavior is very strange, because we're trying to resize the form inside a resize operation. Is there any other way to simulate this behavior?


You might be able to bypass the infinite loop by trying a token setting.

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

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜