简体   繁体   中英

Why won't SizeChanged event work properly with panel?

The code below works properly with every other control I've tried it on, but if I try it on a panel, the event activates only when the form height or width are increased. The panel won't reduce it's size, only augment it.

I'm working with Visual Studio 2012 as my professor asks. The code works with groupboxes but I need to use a panel, can you help me?

private void Program_SizeChanged(object sender, EventArgs e)
{
    controlName.Size = new Size(this.Width - 35, this.Height - 75);
}

Thank you beforehand

SizeChanged

The Resize event occurs when the control is resized, while the SizeChanged event occurs when the Size property changes.

You could use either, as a resize will cause the Size property to change. However, you should rather use the Layout event, as recommended both in the documentation for the Resize and SizeChanged events.

The SizeChanged event is raised by the OnSizeChanged(). This function is in turn only called by UpdateBounds(), which is called by several mechanisms, primarily including the handling of WM_MOVE and WM_WINDOWPOSCHANGED messages.

Again from the source, OnSizeChanged() will only be called when UpdateBounds() has determined that there is a difference between the old size and new size. So, the SizeChanged event corresponds to the Size property changing.

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