Hey guys,

im sort of working on little project and i have these picturebox arrays, so when button1(capture) is pressed the image in the [drawing]picturebox is set to the image of the picturebox, then when you press play, it repeats all the images captured eg if i = 100, it repeats images from 0 -> 100, when i try that i keep on getting object is not a reference or something. can you please help. this is my code


Dim pcindex As Integer = 1 'picturebox number
    Dim c As Integer = 1 'picture box number for timer
    Public pcx As Integer = 20 'creates left coordinate for placing picture boxes next to each other instead of overlaid
    Public pcarray(100) As PictureBox 'creates 100 pictureboxes
    
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        pcarray(pcindex) = New PictureBox 'makes picturebox
        pcarray(pcindex).Name = "PC" & pcindex
        pcarray(pcindex).Left = pcx
        pcarray(pcindex).SizeMode = PictureBoxSizeMode.StretchImage
        pcarray(pcindex).Size = New Size(50, 56)
        pcarray(pcindex).Image = PictureBox1.Image
        pcarray(pcindex).Visible = True
        pcarray(pcindex).BackColor = Color.White
        Controls.Add(pcarray(pcindex)) 'adds picturebox[pcindex]
        pcindex += 1 'adds 1 to pcindex value
        pcx = Val(pcx) + 60 'changes left coordinates

    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

        Try

           

            PictureBox1.image = pcarray(c).image 'make pcarray(c)'s image, picturebox 1's image
            c += 1 'add 1 to c


        Catch es As Exception
            MsgBox(es.Message)
            Timer1.Enabled = False
        End Try
    End Sub

the problem is the code for the timer1 keeps on giving me an error and i need help fixing it. thank you

See if this helps.

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Try
            '// check if Array PictureBox IsNot Nothing, THEN add image from it to your PictureBox1.
            If pcarray(c) IsNot Nothing Then PictureBox1.Image = pcarray(c).Image
            If Not c = pcarray.Length - 1 Then '// if not c ='s the last PictureBox in your PictureBox Array...
                c += 1 'add 1 to c
            Else
                '// either set c back to 0 or disable Timer.
                c = 0
                'Timer1.Enabled = False
            End If
            '/////////////////////////////////////
            '  Me.Text = c.ToString '// FOR TESTING TO VIEW YOUR "c" INDEX.
            '////////////////////////////////////////
        Catch es As Exception
            Timer1.Enabled = False '// set Timer off first, then display MgbBox, else you will get tons of MsgBoxes.
            MsgBox(es.Message)
        End Try
    End Sub

Also, this line of code Public pcarray(100) As PictureBox 'creates 100 pictureboxes declares 101 PictureBoxes, not 100, since Arrays start count at 0, not 1.

Thank you very much

Edit: i have another issue, like just pretend i had 10 pictuboxes when i press play it goes through them and continues. i want it so that when it reaches the last picturebox you drew on, it stops

Next time you "Edit:" a post, notify the replier(s) to your thread by sending them a p.m.(private message). Otherwise, post a new post. This will alert the replier(s) that you or someone else had something else to add to your thread.

Regarding your "Edit:" issue, See if this helps.

Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Static iCounter As Integer = 0
        If Not iCounter = 10 Then '// 10 for 10 PictureBoxes.
            If pcarray(iCounter) IsNot Nothing Then PictureBox1.Image = pcarray(iCounter).Image
            iCounter += 1
        Else
            iCounter = 0 '// reset if needed to restart Timer again.
            Timer1.Stop()
        End If
    End Sub

Try this...

        Dim Shapes(16) As PictureBox
            For i = 1 To 16
                Shapes(i) = New PictureBox
                With Shapes(i)
                    .Name = pbn
                    .BackColor = SystemColors.Control 'Color.Green
                    .BackgroundImage = New Bitmap(My.Resources.led_blk)
                    .BackgroundImageLayout = ImageLayout.Zoom
                    .Size = New Size(20, 20)
                    .Visible = True
                    .Location = New Point( 23 * i, 50)
                End With
                Me.Controls.Add(Shapes(i))
            Next
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.