简体   繁体   中英

Difference between Resize and SizeChanged events

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.

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.

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.

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