Difference between Resize and SizeChanged events
Asked Answered
C

3

41

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?

Cholera answered 16/9, 2011 at 7:26 Comment(0)
S
35

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.

Singhalese answered 16/9, 2011 at 7:31 Comment(4)
But Layout doesn't occure when a resize my controlCholera
@Haider Ali: That's odd. The event is triggered "when the bounds of the control changes" according to this: msdn.microsoft.com/en-us/library/…Singhalese
yes, you are right this link is really helped me, this is exactly what that I am looking for. Thank you very much.Cholera
Great tip on LayoutChanged should be preferred!Halfpint
S
21

Sometimes these answers only come from the source code:

SizeChanged

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.

Resize

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:

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.

LocationChanged and Move

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.

Conclusions

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:

  • When child controls are added/removed.
  • When the bounds of the control changes.
  • When other changes occur that can affect the layout of the control.

    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.

Sinistrad answered 19/1, 2018 at 15:55 Comment(0)
F
13

So what IS the difference between the Resize and the SizeChanged events?

Believe it or not absolutely nothing. One method calls the other. However because the latter is named "Changed" it can be used for data binding - so while I dont have any concrete proof, that's my theory on why both exist.

Fiddlehead answered 16/9, 2011 at 7:33 Comment(2)
yes, that is right, Size is a property and for every property .Net has provided an event for <propertyname>changed event so sizechanged event is associated with its property for data binding. thank you for responseCholera
Well, I found it to be rather different: If I change the Image/BackgroundImage at the end of the Resize event it just doesn't work, as if I had aborted the event. If I move the same code to the SizeChanged everything works as expected.Nesbitt

© 2022 - 2024 — McMap. All rights reserved.