简体   繁体   中英

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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