Search code examples
vb.netdynamically-generatedgroupbox

Adding labels and other controls to a groupbox dynamically in vb.net not working?


So I've been having problems adding labels from a record structure into a groupbox that stack below each other vertically. I could use a Flow Layout Panel but I have multiple labels I need to add from data in a record structure which I want to be able to scroll later with a vertical scrollbar as the autoscroll will only work with one panel only, and not all of them.

For some reason the program puts only one label in, and the others don't seem to be visible even though they have been created (I used a messagebox to check). Could someone please help me to put them in, as I am fairly new to programming and need help.

For context, the program loads 'materials' from an xml file and stores it in a record structure, then this bit of the program creates labels and radio buttons dynamically and puts it in a groupbox, so that it can be arranged manually on a pretty grid that will all become scrollable by using a scrollbar later.. Because the data I am working with is all related, and can have strings of multiple sizes (names and suppliers of materials for example) I didn't want to attempt to add whitespace to make the grid work but with a single label (not that the others would show beneath the first for some reason)

CODE:

'sets up Labels for editing materials
    prgFunctions.loadMat()
    Dim counter As Integer

    Dim newMatIDLabel(numMatFile) As Label
    Dim newMatRdb(numMatFile) As RadioButton
    Dim lastPos As Integer

    'testing to see if materials load GET RID OF LATER
    '  For counter = 1 To numMatFile
    'ListBox1.Items.Add(materials(counter).matName)
    '  Next

    'create the labels with information
    For counter = 1 To numMatFile
        'ID
        newMatIDLabel(counter) = New Label
        newMatIDLabel(counter).Name = "lblMatIDNum" & counter
        newMatIDLabel(counter).Text = materials(counter).matID
        newMatIDLabel(counter).Parent = gbxMaterials


        ' MsgBox(newMatIDLabel(counter).Name & "  " & newMatIDLabel(counter).Text)
    Next

    'create the checkboxes NOW RADIO BUTTONS
    For counter = 1 To numMatFile
        newMatRdb(counter) = New RadioButton
        newMatRdb(counter).Name = "chkMatSelectNum" & counter
        newMatRdb(counter).Text = ""
        newMatRdb(counter).Parent = gbxMaterials
    Next

    'matID locations
    lastPos = 10
    For counter = 1 To numMatFile
        'SOMEHOW MOVE IT INTO THE GROUPBOX INSTEAD, as issues arise everywhere
        newMatIDLabel(counter).Location = New Point(7, lastPos + 10)

        lastPos = lastPos + 10
    Next

Solution

  • Based on F0r3v3r-A-N00b's answer:

    The label has a height of 23, so it seems it is hiding the others.

    This works for me:

        Dim lastPos As Integer = 20
        For counter As Integer = 1 To numMatFile
            Dim label As New Label
            label.Name = "chkMatSelectNum" & counter
            label.Text = materials(counter).matId
            label.AutoSize = True
            label.Visible = True
            label.Location = New Point(7, lastPos)
            gbxMaterials.Controls.Add(label)
            lastPos += 17
        Next