Meximum error number exceeded (VB.NET)

Hi,

I successfully generated several rows of pictureboxes on my form using a click button event with the code below. I ignored the error messages (which said that Pic1 through Pic66 were not declared) and the program worked find.  However when I placed the code in the Form1_load event the boxes did not show on the form. And although I have deleted the button that I previously used to initiate that code it still apear on the form when the program runs. In fact when I click the button (which is supposed to be deleted) the boxes are generated.
Please help

Dim pb As PictureBox
Dim a, b, c As Integer

        Dim x As Integer = 10
        Dim y As Integer = 20
        c = 1
        For b = 1 To 10 ' loop 10 times to make 10 rows
            For a = 1 To 3   ' loop 3 times to make 3 columns
                pb = New PictureBox()
                pb.Name = "Pic" & c  ' c stands for the number to be added to Pic
                pb.Size = New Size(15, 15)
                pb.Location = New Point(20 + x, y)
                pb.BorderStyle = BorderStyle.FixedSingle
                pb.BackColor = Color.White
                x = x + 16
                c = c + 1       ' c is increased by 1 each loop
                Me.Controls.Add(pb)
            Next
            x = 10
            y = y + 18
        Next
        '--------
' Generating 6 rows each row containing 4 boxes
        c = 31
        x = 10
        y = 200
        For b = 1 To 6
            For a = 1 To 4

                pb = New PictureBox()
                pb.Name = "Pic" & c
                pb.Size = New Size(15, 15)
                pb.Location = New Point(20 + x, y)
                pb.BorderStyle = BorderStyle.FixedSingle
                pb.BackColor = Color.White
                x = x + 16
                c = c + 1
                Me.Controls.Add(pb)

            Next
            x = 10
            y = y + 18
        Next
        '----------------
' Generating 3 rows each containing 5 boxes
        ' The Fives
        c = 55
        x = 10
        y = 308
        For b = 1 To 3
            For a = 1 To 5

                pb = New PictureBox()
                pb.Name = "Pic" & c
                pb.Size = New Size(15, 15)
                pb.Location = New Point(20 + x, y)
                pb.BorderStyle = BorderStyle.FixedSingle
                pb.BackColor = Color.White
                x = x + 16
                c = c + 1
                Me.Controls.Add(pb)
            Next
            x = 10
            y = y + 18
        Next
        '-----------
' Generating 1 row with 6 boxes  
        'The six (last) row
        c = 70
        y = 362
        For a = 1 To 6
            pb = New PictureBox()
            pb.Name = "Pic" & c
            pb.Size = New Size(15, 15)
            pb.Location = New Point(20 + x, y)
            pb.BorderStyle = BorderStyle.FixedSingle
            pb.BackColor = Color.White
            x = x + 16
            c = c + 1
            Me.Controls.Add(pb)
        Next
'---------------------

tariqanis2001Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jereteCommented:
How can the button still appear if you've deleted it? *confused*
tariqanis2001Author Commented:
I am confused too, but it does appear once the program runs. Not only that but I have three other buttons (all set to visible) which don't appear once the program runs.
tariqanis2001Author Commented:
I think the explanation for the buttons which refuse to appear could be that the form stops reading the objects on it once the errors are encountered. But maybe we should first be concerned with how can Pic1 through Pic66 be declared.
Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics TeacherCommented:
Are you trying to handle the events of Pic1 thru Pic66?

You can't use a Handles keyword with them because they have been created dynamically.

Instead you would need to use the AddHandler() function each time you create a new PB:

    AddHandler pb.Click, AddressOf pb_Click


Private Sub pb_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    ' this code runs when any of the pb's is clicked
    Dim pb As PictureBox = CType(sender, PictureBox)
    ' do something with pb in here
    ' you can use pb.Name to make a decision
End Sub
tariqanis2001Author Commented:
No, I am not trying to handle the events of Pic1 thru Pic66.
All I am trying to do is to have Pic1 through Pic66 displayed on the form when it is loaded. I can get them displayed when I run the above code from a click button event (by clicking a button that's already on the form) but I can not get them displayed from the Form_Load event.
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics TeacherCommented:
That code works fine for me in the Form_Load() event...   =\

Do you have any other code in the Load() event that you are not showing us?
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics TeacherCommented:
Try creating a New Project with just one Form and your code posted above in the Load() event...does it work there?

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.