Link to home
Start Free TrialLog in
Avatar of tariqanis2001
tariqanis2001

asked on

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
'---------------------

Avatar of jerete
jerete

How can the button still appear if you've deleted it? *confused*
Avatar of tariqanis2001

ASKER

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.
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.
Avatar of Mike Tomlinson
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
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.
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?
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial