In C# .Net in Winforms, I found two events in almost all components, Resize() and SizeChanged(). Is there any difference between them? If both events are the same then why has C# provided two different events?
I am creating a user control in C#. This control contains a text box. I want to resize the textbox when the user changes the control's size. I am confused about which event I should use and why?
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.
Sometimes these answers only come from the source code:
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, yes, as the others have already determined, the SizeChanged
event corresponds to the Size
property changing.
The Resize
event is different, but not by much. It is raised by OnResize()
. This function is called as a direct result of OnSizeChanged()
. However, the reverse is not true. That is to say, OnResize()
DOES NOT call OnSizeChanged()
.
The difference lies in the perspective from which you uses these API's. If you are merely subscribing to these events, then there is virtually no difference. Either way you look at it, whenever the control's size is changed, both events fire.
However, if you are inheriting Control
, one must be aware of the following differences:
OnResize()
will invalidate the control if the style, ControlStyles.ResizeRedraw
is set.OnResize()
will trigger a layout with the AffectedProperty
set to "Bounds"
.The reason why one might care, for instance, is to make sure that their logic is executed before the base implementation (and hence before the invalidate) to be properly drawn to the screen.
These are two move events that parallel SizeChanged
and Resize
almost identically. The exceptions are that OnMove()
only invalidates when the style, ControlStyles.SupportsTransparentBackColor
is true and the backcolor is a less than opaque alpha value. And, OnMove()
will not trigger a layout.
Again this difference likely only matters to control authors.
After investigating into the source code, my conclusion is that the Resize
and Move
events are an abstraction away from the property changed events, and would likely be the preferred choice for both subscribing and overriding these specific events.
As others mention, the Layout
event and OnLayout()
function are also a decent choice; however, layouts can occur in many instances:
Side Note: "Other changes" is vague, but I assume that Microsoft is referring any action taken by inheritors that require a layout.
Relying too strongly on layout events may slow down your code/control since they will occur more frequently than a simple resize event would. Or, it is possible that the layout engine might be suspended by SuspendLayout()
, In this case, you will not be able to react to size changes via the Layout
event.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With