Rate this:
Please Sign up or sign in to vote.
See more:
this is my code

```Private Sub Unitsender_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Unitsender.Tick
Dim TheSquare As New PictureBox 'the box/unit
TheSquare.BackColor = Color.Red 'the units
TheSquare.Width = 15
TheSquare.Height = 20
TheSquare.Visible = True
TheSquare.Location = New Point(Unitmaker.Location.X, Unitmaker.Location.Y - 10)
TheSquare.Location = New Point(TheSquare.Location.X, TheSquare.Location.Y - 10)
Panel1.Controls.Add(TheSquare)
End Sub```

now this is what's ment to happen. when the timer starts the picurebox will load at this other picture but then the picurbox that was made will move foward, think of this as like a space invader game
Posted 26-Apr-12 15:12pm

## 1 solution

Rate this:
Please Sign up or sign in to vote.

## Solution 1

From the requirement given in the question it appears that a `single square` has to move in the `Tick `event of the timer. But as seen from the code given in the question, a new `TheSquare ``PictureBox `is instantiated each time the `Tick `event fires and this new `PictureBox `is being added to the `Panel1`, which makes the `Panel1 `to be cluttered with so many picture boxes. Further, from the code
```TheSquare.Location = New Point(Unitmaker.Location.X, Unitmaker.Location.Y - 10)
TheSquare.Location = New Point(TheSquare.Location.X, TheSquare.Location.Y - 10)```
the second line is redundant, as the Location of `TheSquare `is reassigned by only decrementing the Y value, which could be achieved in the first statement itself by giving `Y - 20`.

So, I think a `PictureBox `can be created at `Class `level and it may be moved by assigning new location in the `Tick `event.
The following sample code may be helpful
```Public Class Form1
Dim TheSquare As New PictureBox()
Dim WithEvents UnitSender As New Timer()
Dim Panel1 As New Panel

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TheSquare.Size = New Size(15, 15)
Panel1.Dock = DockStyle.Fill
TheSquare.BackColor = Color.Red
Panel1.Controls.Add(TheSquare)
Controls.Add(Panel1)
UnitSender.Enabled = True
Me.Size = New Size(100, 125)
End Sub

Private Sub UnitSender_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles UnitSender.Tick
Dim newX As Integer = TheSquare.Location.X
Dim newY As Integer = TheSquare.Location.Y + 10
If newY > (Panel1.Height - TheSquare.Height) Then
newY = 0
newX += 10
If newX > (Panel1.Width - TheSquare.Width) Then
newX = 0
End If
End If
TheSquare.Location = New Point(newX, newY)
End Sub
End Class```
To run the sample, create a `Windows Forms application`, replace the contents of the `Form1.vb` file with the above code and run the application.
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

 0 OriginalGriff 349 1 Nirav Prabtani 268 2 _Amy 185 3 CHill60 170 4 Richard Deeming 135
 0 OriginalGriff 8,104 1 Sergey Alexandrovich Kryukov 7,067 2 Maciej Los 4,039 3 Peter Leow 3,738 4 CHill60 2,912

Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 26 Apr 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100