0

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.

3
  • 4
    Why does it do that? Because this is the documented behavior: "The ResizeEnd event is also generated after the user moves a form, typically by clicking and dragging on the caption bar.". Why did the Microsoft developers make that decision? We don't know. Use the Resize or SizeChanged event instead. You could use a Boolean variable to wait for the resize to end. See an example for C# here. Commented Sep 8, 2022 at 14:11
  • Hi! Thanks for your help! I tried to use the boolean variable through a c# to VB converter, but I don't know how to get it working. I also tried to find VB examples on the internet, but for some reason I'm unable to find any. Strange, I have a hard time believing I'm the first one that wants to implement a resize-only event?? Thanks for any help! Commented Sep 14, 2022 at 12:20
  • I've posted an answer with a complete example below. Commented Sep 14, 2022 at 15:17

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Hi @41686d6564 stands w. Palestine, This is exactly what I was looking for. Thank you VERY much! Kind regards, Eric

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.