none
Top-level control cannot be added to a control

    Question

  • Hello,

    I have a form with a panel on it (among other controls).  I am trying to load picture boxes into the panel (in an attempt to make a star grid).  Using the code below, I get a "Top-level control cannot be added to a control." error on the "Parent = Me.Panel7" line.

    Searching for this error, I came across a solution to change the TopLevelControl property of the panel to FALSE....but I then get an error "TopLevelControl is read only".

    Any help would be appreciated.

    Geo

     

    Public Class frmMain
    
      Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
        LoadPictureBoxes()
    
      End Sub
    
      Private Sub LoadPictureBoxes()
    
        Dim picbox(0 To 16) As PictureBox, i As Integer, LeftStart As Integer, TopStart As Integer
    
        i = 1
        LeftStart = 9
        TopStart = 8
        Do
          picbox(i) = New PictureBox
          With picbox(i)
            Parent = Me.Panel7
            Top = TopStart
            Left = LeftStart
            Width = 24
            Height = 24
            BackColor = Color.LavenderBlush
          End With
          LeftStart = LeftStart + 30
    
          If i Mod 10 Then
            TopStart = TopStart + 30
            LeftStart = 9
          End If
    
          i = i + 1
    
        Loop Until i = 30
    
      End Sub
    
    End Class
    • Edited by geojf Thursday, August 05, 2010 4:03 PM
    Thursday, August 05, 2010 3:34 PM

Answers

  • Geoff

    Here your problem in a more modern way,

    The 16 pictureboxes in two vertical rows on a panel

      Private Sub LoadPictureBoxes()
        Dim picbox(16) As PictureBox
        Dim LeftStart = 9
        Dim TopStart = 8
        For i = 0 To 16
    
          picbox(i) = New PictureBox With {.Top = TopStart, _
                             .Left = LeftStart, .Width = 24, _
                             .Height = 24, .BackColor = Color.LavenderBlush}
    
          Panel7.Controls.Add(picbox(i))
          TopStart += 30
          If i = 8 Then
            TopStart = 8
            LeftStart = 40
          End If
        Next
      End Sub
    

    Success
    Cor
    Thursday, August 12, 2010 8:22 AM
  • add the PictureBoxes to the Panel's Controls collection:

    Panel7.Controls.Add(picbox(i))
    

    and take out the Parent = Me.Panel7 line

    and in your With block, each line should start with a period

    With picbox(i)
      .Top = TopStart
    

     

    and your  If i  Mod 10  line will fail. The Mod operator returns an integer, not a Boolean value

    Friday, August 06, 2010 12:20 AM

All replies

  • Can you paste in your code again using the blue </> button while you are in edit mode.

    It is really unusable to understand your code.

     


    Success
    Cor
    Thursday, August 05, 2010 3:48 PM
  • I dont know what you mean by asking me to use   the blue </> button

    Geo

     

    Ah...now I get it...you meant the HTML button

    Thursday, August 05, 2010 3:51 PM
  • I dont know what you mean by asking me to use   the blue </> button

    Geo

     

    Ah...now I get it...you meant the HTML button


    No, Cor means the "Insert code block" button which is to the right of the "Edit HTML source" button. It has a Blue </> superimposed on a black outline of a window (nobody knows why).
    Thursday, August 05, 2010 4:10 PM
  • I dont know what you mean by asking me to use   the blue </> button

    Geo

     

    Ah...now I get it...you meant the HTML button


    No they mean the  button in the editor .
    Coding4fun Be a good forum member mark posts that contain the answers to your questions or those that are helpful
    Please format the code in your posts with the button . Makes it easier to read . Or use the Forum Code Formatter by JohnWein http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/bf977a14-d9d4-4e84-9784-bf76b9e23261
    Thursday, August 05, 2010 8:52 PM
  • add the PictureBoxes to the Panel's Controls collection:

    Panel7.Controls.Add(picbox(i))
    

    and take out the Parent = Me.Panel7 line

    and in your With block, each line should start with a period

    With picbox(i)
      .Top = TopStart
    

     

    and your  If i  Mod 10  line will fail. The Mod operator returns an integer, not a Boolean value

    Friday, August 06, 2010 12:20 AM
  • Hello geojf,

    Thanks for your post.

    What about your problem? jwavila has given you helpful suggestion. Please have a try. In addition, the loop index was outside the bounds of the array. So change Loop Until i=30 to Loop Until i=16. Hope it helpful.

    If you have any comments, please feel free to follow up.

    Best regards,
    Liliane
    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please mark the replies as answers if they help and unmark them if they provide no help. Welcome to the All-In-One Code Framework! If you have any feedback, please tell us. Thanks
    Thursday, August 12, 2010 6:31 AM
  • Geoff

    Here your problem in a more modern way,

    The 16 pictureboxes in two vertical rows on a panel

      Private Sub LoadPictureBoxes()
        Dim picbox(16) As PictureBox
        Dim LeftStart = 9
        Dim TopStart = 8
        For i = 0 To 16
    
          picbox(i) = New PictureBox With {.Top = TopStart, _
                             .Left = LeftStart, .Width = 24, _
                             .Height = 24, .BackColor = Color.LavenderBlush}
    
          Panel7.Controls.Add(picbox(i))
          TopStart += 30
          If i = 8 Then
            TopStart = 8
            LeftStart = 40
          End If
        Next
      End Sub
    

    Success
    Cor
    Thursday, August 12, 2010 8:22 AM