Fancy Collapsing Form Exit

Level:
Level1

You can always close your current form with the Unload Visual Basic command. When that line is encountered your form will disappear. However, wouldn't it be more exciting if instead of just disappearing your form collapsed in towards the center of itself and then disappeared. The way we do this is by setting the forms width and height to a smaller amount over and over again. This can be done easily with a Do while loop. The important thing is to make sure you stop collapsing the form before it gets too small. You will get an error if you set the width or height to anything less than 0.

To try this out create a Visual Basic 6 project and a command button to your main form. Double click on the command and enter the following code in the click event handler for your button.

  1. Do While Me.Width > 1800
  2.         Me.Width = Me.Width - 300
  3.         Me.Left = Me.Left + 150
  4.         If Me.Height - 400 Then
  5.                 Me.Top = Me.Top - 1
  6.                 Me.Height = Me.Height - 150
  7.         End If
  8. Loop
  9. Unload Form1: End

Once you have the code entered run your program and click the button. You should see your form leave with a little style

Note: The source for this was found at DreamVB which is no longer online.

If you enjoyed this post, subscribe for updates (it's free)

P.S.

Add this:
If Me.Left > Screen.Width Then End
and your form will automatically close if it flew out of the screen.

Congratulations!!!

I like your codes

bravo

guys you were all wonderful, I am thankful I have found you all. One of this days I will be posting some of my works which I have learned from you all with a little bit modification from my creativity...Thanx again to all of you

Open & Close your Forms Smoothly

I have made the following code for a nice and smooth opening and closing event for any form:

Option Explicit
Dim X As Integer, Y As Integer

Private Sub Form_Load()
X = Me.Width
Y = Me.Height
Me.Width = 0
Me.Height = 0
Me.Left = (Screen.Width - Me.Width) / 2
Me.Top = (Screen.Height - Me.Height) / 2
End Sub

Private Sub Form_Unload(Cancel As Integer)
Timer2.Enabled = True
Cancel = True
End Sub

Private Sub Timer1_Timer() 'Timer for the opening event
If Me.Width < X Then
Me.Left = (Screen.Width - Me.Width) / 2
Me.Width = Me.Width + 400
Exit Sub
ElseIf Me.Width > X Then
Me.Width = X
End If

If Me.Height < Y Then
Me.Top = (Screen.Height - Me.Height) / 2
Me.Height = Me.Height + 350
Exit Sub
ElseIf Me.Height > Y Then
Me.Height = Y
End If
Timer1.Enabled = False
End Sub

Private Sub Timer2_Timer() 'Timer for the closing event
If Me.Height > 405 Then
Me.Top = (Screen.Height - Me.Height) / 2
Me.Height = Me.Height - 400
Exit Sub
End If

If Me.Width > 1680 Then
Me.Left = (Screen.Width - Me.Width) / 2
Me.Width = Me.Width - 400
Exit Sub
End If
End
End Sub

You need a form and two timers to work the above project.
Timer1.Enabled must be True when the form loads and Timer2.Enabled False.
You can use 15 for both timer intervals.
I hope you like it!

A Correction for the Close event

My preivious code has a disadvantage when you close the form. If you move the form to a new position and try to close it from there, the close event will center the form to the middle of the screen. Not so fancy...
Also if you maximize the form, a run-time error will occur!
To correct these errors, replace the following events:

Private Sub Form_Unload(Cancel As Integer)
If Me.WindowState = vbMaximized Then End
Timer2.Enabled = True
Cancel = True
End Sub

Private Sub Timer2_Timer() 'Timer for the closing event
If Me.WindowState = vbMaximized Then Exit Sub
If Me.Height > 405 Then
Me.Height = Me.Height - 300
Me.Top = Me.Top + 150
Exit Sub
End If
If Me.Width > 1680 Then
Me.Width = Me.Width - 300
Me.Left = Me.Left + 150
Exit Sub
End If
End
End Sub

Thanks :)

Hey man, I'm totally using this in my program! It's a great piece of code!
Thanks so much! It's really helpful ;D

New

I made a new one, this one is better.

    Dim i As Long
    Me.Top = (Screen.Height / 2) - (Me.Height / 2)
    Me.Left = (Screen.Width / 2) - (Me.Width / 2)
 
    For i = Me.Left To (Screen.Width / 2) Step 10
        Me.Height = Me.Height - 15
        Me.Width = Me.Width - 20
        Me.Left = Me.Left + 100
        DoEvents
    Next    
    Unload Me

AWESOME

THAT IS THE COOLEST THING EVAR!!!!!1!!!

hehe i liked it, good one,

hehe
i liked it, good one, but problem is that it cant be apllied when form is maximized... :-(

add this line on top of the

add this line on top of the other code then your form can be maximized:
Me.WindowState = 0

nice work! i really like it

nice work! i really like it thank u

update

i think its better to add this two lines:

Me.Refresh
DoEvents

because if you wont add this , it will hang up your pc

cheerss!!!

LIKE! LIKe! liKe! LIKE!!!!!

LIKE! LIKe! liKe! LIKE!!!!! ;D juSt goRgeOUs

I went ahead and added that

I went ahead and added that into it:

Do While Me.Width > 1800
Me.Width = Me.Width - 300
Me.Left = Me.Left + 150
If Me.Height - 400 Then
Me.Top = Me.Top - 1
Me.Height = Me.Height - 150
End If
Me.Refresh
DoEvents
Loop
Unload Form1: End

update

If you add the following two lines before the close of the Loop statement, it will be more streamlined (e.g. you wont see the trail of the window shrinking into itself):

]
Me.Refresh
DoEvents

Cheers!
- AP