Home > Enterprise >  Why does a form move trigger ResizeEnd?
Why does a form move trigger ResizeEnd?

Time:09-15

I use the following code in my form:

Public Class Form1
    Private Sub Form1_ResizeEnd(sender As Object, e As EventArgs) Handles MyBase.ResizeEnd
        MsgBox("Resized")
    End Sub
End Class

When I move my form, it also seems to trigger MyBase.ResizeEnd. Why is that? A move of the panel doesn't change the size, so I don't understand why.

CodePudding user response:

Why does a form move trigger ResizeEnd?

Because this is the documented behavior. From the documentation:

The ResizeEnd event is also generated after the user moves a form, typically by clicking and dragging on the caption bar.

If you want an event that doesn't get triggered when the form is moved, you should use either Resize or SizeChanged. The problem with those two events is that they will be triggered while the form is being resized by the user. To work around that, you may use it with both ResizeBegin and ResizeEnd with a couple of flags to signal when the user actually finishes resizing the form.

Here's a complete example:

Private _resizeBegin As Boolean
Private _sizeChanged As Boolean

Private Sub Form1_ResizeBegin(sender As Object, e As EventArgs) Handles MyBase.ResizeBegin
    _resizeBegin = True
End Sub

Private Sub Form1_SizeChanged(sender As Object, e As EventArgs) Handles MyBase.SizeChanged
    ' This is to avoid registering this as a resize event if it was triggered
    ' by another action (e.g., when the form is first initialized).
    If Not _resizeBegin Then Exit Sub

    _sizeChanged = True
End Sub

Private Sub Form1_ResizeEnd(sender As Object, e As EventArgs) Handles MyBase.ResizeEnd
    _resizeBegin = False
    If _sizeChanged Then
        _sizeChanged = False
        MessageBox.Show("The form has been resized.")
    End If
End Sub

One thing to note is that both ResizeBegin and ResizeEnd are only triggered when the user manually resizes* the form. It does not, however, handle other situations like when the form is resized via code, when the form is maximized, or restored.


* or moves the form, which is the part that we're trying to avoid here.

  • Related