Public Class Form1
Private rect As New Rectangle(0, 0, 15, 15)
Private xincrement As Integer = 5
Private yincrement As Integer = 0
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Timer1.Start()
End Sub
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
'Draw the Rectangle.
e.Graphics.DrawRectangle(Pens.White, Me.rect)
'Fill the rectangle.
e.Graphics.FillRectangle(Brushes.White, Me.rect)
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
'Mark the old rectangle to be erased.
Me.Invalidate(Rectangle.Inflate(Me.rect, 1, 1))
'Shift the rectangle.
Me.rect.Offset(Me.xincrement, Me.yincrement)
'Mark the new rectangle to be drawn.
Me.Invalidate(Rectangle.Inflate(Me.rect, 1, 1))
'Do the drawing.
Me.Update()
'Change the direction of movement if a boundary has been reached.
If Me.xincrement = 1 AndAlso Me.rect.Right >= Me.ClientSize.Width - 1 Then
Me.xincrement = 0
Me.yincrement = 1
ElseIf Me.yincrement = 1 AndAlso Me.rect.Bottom >= Me.ClientSize.Height - 1 Then
Me.xincrement = -1
Me.yincrement = 0
End If
End Sub
End Class