Click here to Skip to main content
15,881,687 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
this is my code

VB
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

1 solution

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
VB
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
VB
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.
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900