Need Help with Resize Event

Jean Vallee 21 Reputation points
2020-11-15T16:45:05.203+00:00

I am struggling with the Resize event. I have a 3 tabpage form with 300+ plus controls.
I am not using anchors or TableLayoutPanel, but am calculating the new size and placement manually for each control in the Resize event.
I allow the user to maximize form from the design size of 1024x768. My resize event starts with SuspendLayout, calculates the new size and location of each control. Calculation is based on ratio of previous window size to new window size. Then I have ResumeLayout. In tracing triggered events, Resize, and most others affected by a window resize fire multiple times. For example: all these fire in the order shown when the form is called at full screen: (I'm not sure why they fire multiple times)

  1. size changed event
  2. clientsizechanged event,
  3. size changed event
  4. clientsizechanged event
  5. load event
  6. location changed event
  7. location changed event
  8. location changed event
  9. resize event
  10. size changed event
  11. activated event
  12. paint event
  13. shown event
  14. location changed event
  15. resize event
  16. size changed event
  17. paint event

What event fires a) only ONCE and 2) AFTER all resize events? It seems to me that I should move the ResumeLayout command from the Resize event to whatever is absolutely last so the form is not redrawn multiple times.

Developer technologies Windows Forms
Developer technologies Visual Studio Other
{count} votes

2 answers

Sort by: Most helpful
  1. Viorel 122.5K Reputation points
    2020-11-15T17:27:10.853+00:00

    The frequency of Resize event seems to depend on “Show window contents while dragging” option from Settings (searching for “performance options”).

    Did you consider ResizeBegin and ResizeEnd event instead of Resize?

    To avoid premature operations performed on Size, you can use a boolean flag which is set in Load event.

    By the way, to circumvent accumulation of calculation errors, it is probably better to base the calculation on sizes saved on Load event, not on previous window size.


  2. Daniel Zhang-MSFT 9,651 Reputation points
    2020-11-17T02:23:50.85+00:00

    Hi Jean Vallee,
    >>My resize event starts with SuspendLayout, calculates the new size and location of each control. Calculation is based on ratio of previous window size to new window size. Then I have ResumeLayout.
    When adding several controls to a parent control, it is recommended that you call the SuspendLayout method before initializing the controls to be added. After adding the controls to the parent control, call the ResumeLayout method.
    >> form opens at 1024x758 and double-click title bar to maximize. That's as far as I've gotten. The ResizeBegin and ResizeEnd events do not fire.
    The ResizeBegin event is raised when the user begins to resize a form, typically by clicking and dragging one of the borders or the sizing grip located on the lower-right corner of the form.
    This action puts the form into a modal sizing loop until the resize operation is completed. Typically, the following set of events occurs during a resize operation:

    1. A single ResizeBegin event occurs as the form enters resizing mode.
    2. Zero or more pairs of Resize and SizeChanged events occur as the form's Size is modified.
    3. A single ResizeEnd event occurs as the form exits resizing mode.
      And you also need to note that just clicking without dragging on a border or resizing grip will generate the ResizeBegin and ResizeEnd events without any intermediate Resize and SizeChanged event pairs.
      So when you double-click title bar to maximize, it will only trigger the resize size change events.
      Here are some related links you can refer to.
      Difference between Resize and SizeChanged events
      How to change the size of the child controls in a composite control in winforms?
      Hope these are helpful to you.
      Best Regards,
      Daniel Zhang

    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.