hi. soo i got this codes off of some site.
its for a cool form entry and exit effect.
but since im using this in my project i need to know what this lines of codes mean.
and i mean every line.
the site where i got it from doesnt offer this info which i need.
im hoping that you guys could help me out with this.
i need to explain every line of code in class.

you will need :
2 timers for this.
first timers set to true. the others false. each with 15 interval.

heres the code.

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 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()
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
End Sub

Dani AI

Generated

A few focused clarifications and fixes that build on ’s helpful line-by-line notes and fill the gaps that matter when explaining this to a class.

Option Explicit is correct—force declarations so typos become compile errors. Important: form dimensions in classic VB are measured in twips by default (1 twip = 1/1440 inch), so values easily exceed the 16‑bit Integer range. Use Long for X/Y to avoid overflow:

Dim X As Long, Y As Long

Screen.Width/Height and Me.Width/Height use the same units, so the centering math (screen minus form, divided by two) is valid. If pixel coordinates are preferred, convert with Screen.TwipsPerPixelX/Y or set the form ScaleMode to pixels before measuring.

Timer behavior and animation quality: a Timer interval of 15ms is aggressive and Windows message timers are not frame‑perfect, so smoothness comes from sensible step sizes combined with the interval. Avoid very large per‑tick jumps if the goal is smooth motion; smaller increments per tick produce a cleaner effect. Recomputing Left/Top to re‑center after each resize is fine but do the size change and then compute position so the center stays stable.

Critical bugs and safety:

  • The closing routine contains a mistaken conditional (looks like a typo). It should compare height properly (e.g. If Me.Height > 400 Then).
  • The tight Do While loop with DoEvents inside a Timer tick is brittle and allows reentrancy. Prefer driving the shrink/slide from the timer ticks (one step per Tick) and disable the timer when done.
  • Use Unload Me (or Unload Form1) instead of an unconditional End, which kills the process without clean shutdown.

For a classroom explanation, annotate each line with: purpose, units/types involved, side effects (screen repaint, blocking), and a suggested “what could go wrong” note. Mention alternatives (API calls like AnimateWindow/SetWindowPos for smoother effects) and point out that testing on different screen DPIs/ScaleMode settings is important.

Recommended Answers

All 2 Replies

Option Explicit
Dim X As Integer, Y As Integer 'Declare variable X and Y as Integer 

Private Sub Form_Load()
    ' This following code to assing variable X and Y with Width and Height of Form
    X = Me.Width 'assign variable X with form Width 
    Y = Me.Height 'assign variable Y with form Height
	
    'This following code to set Width and height of Form To 0
    Me.Width = 0 ' Set form Width to 0
    Me.Height = 0 ' Set form Height to 0
	
    ' This following to centered form
    Me.Left = (Screen.Width - Me.Width) / 2 ' Set Left with (Width of your computer screen -  Width of form) then Divide it by 2
    Me.Top = (Screen.Height - Me.Height) / 2 ' Set Top with (Height of your computer screen -  Height of form) then Divide it by 2
End Sub

'THis for Opened Form Animation
Private Sub Timer1_Timer()
 'Timer for the opening event
' This following code will  bringin back Form Size.. 
' form will increase width by 400 and Height by 350. So it will seems like animation..

' This following code will bring the Form Width Size
If Me.Width < X Then ' If Width of Form < X  (X is Form Width. Look at asiggnment in form load)
	Me.Left = (Screen.Width - Me.Width) / 2 'Set Left with (Width of your computer screen -  Width of form) then Divide it by 2. This for centered form
	Me.Width = Me.Width + 400 ' Form Width will increase by 400 until Width => X
	Exit Sub ' Exit this sub if form width is same or more than X
ElseIf Me.Width > X Then 'Else when increasing form width if with more than X then Set to X
	Me.Width = X ' Set Form width to X
End If

' This following code will bring the Form Width Size
' Increasing of Height has same explanation like Width
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 ' Disable Timer (Stop)
End Sub

'THis for Closing Form Animation
Private Sub Timer2_Timer()
Do While Me.Width > 1800 'this event will running until Form Width 
	Me.Width = Me.Width - 300 ' Form Width size will decrease by 300
	Me.Left = Me.Left + 150 ' Form Left will increase by 150. So form seems like moving to right
	If Me.Height - 400 Then '
		Me.Top = Me.Top - 1 ' Form Top will decrease by 1
		Me.Height = Me.Height - 150 'Form height size will decrease by 150
	End If
	Me.Refresh ' refresh form
	DoEvents
Loop  ' loop with condition
Unload Form1: End 'closing form
End Sub
commented: Very good explanation +2
Option Explicit
Dim X As Integer, Y As Integer 'Declare variable X and Y as Integer 

Private Sub Form_Load()
    ' This following code to assing variable X and Y with Width and Height of Form
    X = Me.Width 'assign variable X with form Width 
    Y = Me.Height 'assign variable Y with form Height
	
    'This following code to set Width and height of Form To 0
    Me.Width = 0 ' Set form Width to 0
    Me.Height = 0 ' Set form Height to 0
	
    ' This following to centered form
    Me.Left = (Screen.Width - Me.Width) / 2 ' Set Left with (Width of your computer screen -  Width of form) then Divide it by 2
    Me.Top = (Screen.Height - Me.Height) / 2 ' Set Top with (Height of your computer screen -  Height of form) then Divide it by 2
End Sub

'THis for Opened Form Animation
Private Sub Timer1_Timer()
 'Timer for the opening event
' This following code will  bringin back Form Size.. 
' form will increase width by 400 and Height by 350. So it will seems like animation..

' This following code will bring the Form Width Size
If Me.Width < X Then ' If Width of Form < X  (X is Form Width. Look at asiggnment in form load)
	Me.Left = (Screen.Width - Me.Width) / 2 'Set Left with (Width of your computer screen -  Width of form) then Divide it by 2. This for centered form
	Me.Width = Me.Width + 400 ' Form Width will increase by 400 until Width => X
	Exit Sub ' Exit this sub if form width is same or more than X
ElseIf Me.Width > X Then 'Else when increasing form width if with more than X then Set to X
	Me.Width = X ' Set Form width to X
End If

' This following code will bring the Form Width Size
' Increasing of Height has same explanation like Width
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 ' Disable Timer (Stop)
End Sub

'THis for Closing Form Animation
Private Sub Timer2_Timer()
Do While Me.Width > 1800 'this event will running until Form Width 
	Me.Width = Me.Width - 300 ' Form Width size will decrease by 300
	Me.Left = Me.Left + 150 ' Form Left will increase by 150. So form seems like moving to right
	If Me.Height - 400 Then '
		Me.Top = Me.Top - 1 ' Form Top will decrease by 1
		Me.Height = Me.Height - 150 'Form height size will decrease by 150
	End If
	Me.Refresh ' refresh form
	DoEvents
Loop  ' loop with condition
Unload Form1: End 'closing form
End Sub

thankyou so much!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.