none
Creating an array of pictureboxes and moving them left and right without going off the page. RRS feed

  • Question

  • I'm really struggling to make an array of picture boxes and moving them all at the same time, at runtime but it doesn't seem to work so I have tried moving the individually but they move one after the other.

    This is the code that I have tried.

     Dim movekron As Integer = 6
        Dim oppositeDirection As Integer = -1
    
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            PlayerMovement()
            If PictureBox1.Location.X = Me.Width - PictureBox1.Width Then
                movekron = movekron * oppositeDirection
            ElseIf PictureBox1.Location.X < 0 Then
                movekron = movekron * oppositeDirection
            End If
        End Sub
    
        Private Sub PlayerMovement()
    #Region "MOVE KRONS"
            PictureBox1.Location = New Point(PictureBox1.Location.X + movekron, PictureBox1.Location.Y)
            PictureBox2.Location = New Point(PictureBox2.Location.X + movekron, PictureBox2.Location.Y)
            PictureBox3.Location = New Point(PictureBox3.Location.X + movekron, PictureBox3.Location.Y)
            PictureBox4.Location = New Point(PictureBox4.Location.X + movekron, PictureBox4.Location.Y)
            PictureBox5.Location = New Point(PictureBox5.Location.X + movekron, PictureBox5.Location.Y)
            PictureBox6.Location = New Point(PictureBox6.Location.X + movekron, PictureBox6.Location.Y)
            PictureBox7.Location = New Point(PictureBox7.Location.X + movekron, PictureBox7.Location.Y)
            PictureBox8.Location = New Point(PictureBox8.Location.X + movekron, PictureBox8.Location.Y)
            PictureBox9.Location = New Point(PictureBox9.Location.X + movekron, PictureBox9.Location.Y)
            PictureBox10.Location = New Point(PictureBox10.Location.X + movekron, PictureBox10.Location.Y)
            PictureBox11.Location = New Point(PictureBox11.Location.X + movekron, PictureBox11.Location.Y)
            PictureBox12.Location = New Point(PictureBox12.Location.X + movekron, PictureBox12.Location.Y)
            PictureBox13.Location = New Point(PictureBox13.Location.X + movekron, PictureBox13.Location.Y)
            PictureBox14.Location = New Point(PictureBox14.Location.X + movekron, PictureBox14.Location.Y)
            PictureBox15.Location = New Point(PictureBox15.Location.X + movekron, PictureBox15.Location.Y)
            PictureBox16.Location = New Point(PictureBox16.Location.X + movekron, PictureBox16.Location.Y)
            PictureBox17.Location = New Point(PictureBox17.Location.X + movekron, PictureBox17.Location.Y)
            PictureBox18.Location = New Point(PictureBox18.Location.X + movekron, PictureBox18.Location.Y)
            PictureBox19.Location = New Point(PictureBox19.Location.X + movekron, PictureBox19.Location.Y)
            PictureBox20.Location = New Point(PictureBox20.Location.X + movekron, PictureBox20.Location.Y)
            PictureBox21.Location = New Point(PictureBox21.Location.X + movekron, PictureBox21.Location.Y)
            PictureBox22.Location = New Point(PictureBox22.Location.X + movekron, PictureBox22.Location.Y)
    #End Region
        End Sub
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Me.FormBorderStyle = Windows.Forms.FormBorderStyle.None
            Me.WindowState = FormWindowState.Maximized
            Timer1.Start()
    #Region "KRONSTARTINGPOINTS"
            PictureBox1.Location = New Point(179, 38) ''
            PictureBox2.Location = New Point(285, 38)
            PictureBox3.Location = New Point(391, 38)
            PictureBox4.Location = New Point(497, 38)
            PictureBox5.Location = New Point(603, 38)
            PictureBox6.Location = New Point(709, 38)
            PictureBox7.Location = New Point(815, 38)
            PictureBox8.Location = New Point(921, 38)
            PictureBox9.Location = New Point(1027, 38)
            PictureBox10.Location = New Point(1133, 38)
            PictureBox11.Location = New Point(1239, 38)
            PictureBox12.Location = New Point(179, 150) ''
            PictureBox13.Location = New Point(285, 150)
            PictureBox14.Location = New Point(391, 150)
            PictureBox15.Location = New Point(497, 150)
            PictureBox16.Location = New Point(603, 150)
            PictureBox17.Location = New Point(709, 150)
            PictureBox18.Location = New Point(815, 150)
            PictureBox19.Location = New Point(921, 150)
            PictureBox20.Location = New Point(1027, 150)
            PictureBox21.Location = New Point(1133, 150)
            PictureBox22.Location = New Point(1239, 150)
    #End Region
        End Sub
    End Class

    I was able to move a 3 <g class="gr_ gr_322 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" data-gr-id="322" id="322">picturebox</g> back and forth but when I try adding move it starts lagging

    Public Class Form1
        Dim movekron As Integer = 1.5
        Dim oppositeDirection As Integer = -1
    
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            PlayerMovement()
            If PictureBox1.Location.X = Me.Width - PictureBox1.Width Then
                movekron = movekron * oppositeDirection
            ElseIf PictureBox1.Location.X < 0 Then
                movekron = movekron * oppositeDirection
            End If
            PictureBox1.Location = New Point(PictureBox1.Location.X + movekron, PictureBox1.Location.Y)
        End Sub
        Private Sub PlayerMovement()
            PictureBox1.Location = New Point(PictureBox1.Location.X + movekron, PictureBox1.Location.Y)
        End Sub
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Timer1.Start()
        End Sub
    End Class

    Could <g class="gr_ gr_450 gr-alert gr_tiny gr_spell gr_inline_cards gr_run_anim ContextualSpelling multiReplace" data-gr-id="450" id="450">i</g> please have some help. I would really appreciate it.

    Thank you

    Friday, March 22, 2019 5:14 PM

All replies

  • Hi

    Just a thought. Why not put all the PictureBoxes in a container (Panel say) and just move the panel (which will move all the PictureBoxes together).

    Without a full knowledge3 of your application, faced with that quetion, this is what springs to my mind.


    Regards Les, Livingston, Scotland

    Friday, March 22, 2019 6:58 PM
  • Not sure what you want to do, but if you want to move pictures without jerks, you should use Direct2D ID2D1Bitmap
    For example, a test with 2 sprites (with different speeds) : VB_Direct2D.exe


    • Edited by Castorix31 Friday, March 22, 2019 8:06 PM
    Friday, March 22, 2019 7:45 PM
  • Here is an example that makes a List of Pictureboxes. Then loops the list to move the pictureboxes to the new locations.

    The example makes the controls just cut and paste into an empty form (change name as reqd).

    Public Class Form1
        Private WithEvents Timer1 As New System.Windows.Forms.Timer With {.Interval = 100, .Enabled = False}
        Private SpriteSize As Integer = 20
        Private SpritesPt As New Point(SpriteSize, SpriteSize)
        Private Sprites As New List(Of PictureBox)
        Private Direction As Integer = -1
        Private Msg As String = "Click to Play..."
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            DoubleBuffered = True
    
            For i As Integer = 1 To 7
                Sprites.Add(New PictureBox With {.Parent = Me, .Name = "Sprite" & i.ToString,
                    .BackColor = Color.Red, .Size = New Size(SpriteSize, SpriteSize)})
            Next
        End Sub
    
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            'move the sprite locations
            SpritesPt.X += Direction * 10
    
            'check for edge of screen
            If SpritesPt.X > ClientSize.Width - (SpriteSize + (SpriteSize * (Sprites.Count - 1))) Or
                    SpritesPt.X < SpriteSize Then
                'move the sprites down
                SpritesPt.Y += SpriteSize
                Direction = -Direction
                Dim dt As Integer = Timer1.Interval - 20
                If dt < 10 Then dt = 10
                Timer1.Interval = dt
            End If
    
            'move the sprite pictureboxes to the new location
            Dim x, y As Integer
            For i As Integer = 0 To Sprites.Count - 1
                x = SpritesPt.X + (i * SpriteSize)
                If i Mod 2 = 0 Then y = SpritesPt.Y Else y = SpritesPt.Y + SpriteSize
                Sprites(i).Location = New Point(x, y)
            Next
    
            'check for end of game
            If SpritesPt.Y > ClientSize.Height - (3 * SpriteSize) Then
                Timer1.Stop()
                Msg = "Game Over" & vbLf & "Click to Play..."
                SpritesPt = New Point(SpriteSize, SpriteSize)
                Timer1.Interval = 100
                Invalidate()
            End If
        End Sub
    
        Private Sub Form5_Click(sender As Object, e As EventArgs) Handles Me.Click
            Timer1.Enabled = Not Timer1.Enabled
            If Not Timer1.Enabled Then Msg = "Click to Continue..."
            Invalidate()
        End Sub
    
        Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
            With e.Graphics
                .Clear(Color.LightSkyBlue)
    
                .FillEllipse(Brushes.Sienna, New Rectangle(0, CInt(0.7 * ClientSize.Height),
                                                    ClientSize.Width, CInt(0.7 * ClientSize.Height)))
    
                If Not Timer1.Enabled Then
                    e.Graphics.DrawString(Msg, New Font("tahoma", 24, FontStyle.Bold),
                                          Brushes.LightGoldenrodYellow, 20, 20)
                End If
            End With
        End Sub
    
        Private Sub Form1_Resize(sender As Object, e As EventArgs) Handles Me.Resize
            Invalidate()
        End Sub
    End Class

    Saturday, March 23, 2019 9:03 AM
  • <g class="gr_ gr_3 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" data-gr-id="3" id="3">Im</g> making a space invaders game so the <g class="gr_ gr_77 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling" data-gr-id="77" id="77">pictureboxes</g> will have to be individual so that a laser can collide with it.

    I have shown you how. Don't you understand the example? See the list of pictureboxes made in form load.

    Go delete the duplicate post of this question that you just made.

    If you have further questions related to this thread ask them here in this thread.

    Saturday, March 23, 2019 11:29 AM
  • <g class="gr_ gr_3 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" data-gr-id="3" id="3">Im</g> making a space invaders game so the <g class="gr_ gr_77 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling" data-gr-id="77" id="77">pictureboxes</g> will have to be individual so that a laser can collide with it.

    I have shown you how. Don't you understand the example? See the list of pictureboxes made in form load.

    Go delete the duplicate post of this question that you just made.

    If you have further questions related to this thread ask them here in this thread.

    'Theres an error wherever Timer1

    Public Class Form1
        Private WithEvents Timer1 As New System.Windows.Forms.Timer With {.Interval = 100, .Enabled = False}
        Private SpriteSize As Integer = 20
        Private SpritesPt As New Point(SpriteSize, SpriteSize)
        Private Sprites As New List(Of PictureBox)
        Private Direction As Integer = -1
        Private Msg As String = "Click to Play..."
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            DoubleBuffered = True
    
            For i As Integer = 1 To 7
                Sprites.Add(New PictureBox With {.Parent = Me, .Name = "Sprite" & i.ToString,
                    .BackColor = Color.Red, .Size = New Size(SpriteSize, SpriteSize)})
            Next
        End Sub
    
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            'move the sprite locations
            SpritesPt.X += Direction * 10
    
            'check for edge of screen
            If SpritesPt.X > ClientSize.Width - (SpriteSize + (SpriteSize * (Sprites.Count - 1))) Or
                    SpritesPt.X < SpriteSize Then
                'move the sprites down
                SpritesPt.Y += SpriteSize
                Direction = -Direction
                Dim dt As Integer = Timer1.Interval - 20
                If dt < 10 Then dt = 10
                Timer1.Interval = dt
            End If
    
            'move the sprite pictureboxes to the new location
            Dim x, y As Integer
            For i As Integer = 0 To Sprites.Count - 1
                x = SpritesPt.X + (i * SpriteSize)
                If i Mod 2 = 0 Then y = SpritesPt.Y Else y = SpritesPt.Y + SpriteSize
                Sprites(i).Location = New Point(x, y)
            Next
    
            'check for end of game
            If SpritesPt.Y > ClientSize.Height - (3 * SpriteSize) Then
                Timer1.Stop()
                Msg = "Game Over" & vbLf & "Click to Play..."
                SpritesPt = New Point(SpriteSize, SpriteSize)
                Timer1.Interval = 100
                Invalidate()
            End If
        End Sub
    
        Private Sub Form5_Click(sender As Object, e As EventArgs) Handles Me.Click
            Timer1.Enabled = Not Timer1.Enabled
            If Not Timer1.Enabled Then Msg = "Click to Continue..."
            Invalidate()
        End Sub
    
        Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
            With e.Graphics
                .Clear(Color.LightSkyBlue)
    
                .FillEllipse(Brushes.Sienna, New Rectangle(0, CInt(0.7 * ClientSize.Height),
                                                    ClientSize.Width, CInt(0.7 * ClientSize.Height)))
    
                If Not Timer1.Enabled Then
                    e.Graphics.DrawString(Msg, New Font("tahoma", 24, FontStyle.Bold),
                                          Brushes.LightGoldenrodYellow, 20, 20)
                End If
            End With
        End Sub
    
        Private Sub Form1_Resize(sender As Object, e As EventArgs) Handles Me.Resize
            Invalidate()
        End Sub
    End Class


    Sunday, March 24, 2019 10:26 AM
  • J0,

    "'Theres an error wherever Timer1"

    What is the error?

    You don't need to add a timer control to the form the code makes the timer here:

    Private WithEvents Timer1 As New System.Windows.Forms.Timer With {.Interval = 100, .Enabled = False}
        

    If you have added your own timer you can remove the above line or change the names or remove the one you added.

    If you get some other error then tell us exactly what the error is ie do you get a message? What line?


    Sunday, March 24, 2019 11:35 AM