Question Dynamically creating controls on form, but not visible.

JA12

Member
Joined
Jul 2, 2013
Messages
17
Location
Ireland
Programming Experience
10+
I have a basic form "template" that I load with controls from an XML file.

The structure of the "template" is

TabControl
1 TabPage
2 GroupBox Columns on TabPage.

The dynamic Controls that are loaded are

1 (or more) GroupBox containers
1 (or more) GroupBox container with (6 buttons, 4 texboxes, 2 labels)

I create each control and "Add" to the Container control

Each control as the properties

oControl.Visible = True
oControl.BringToFront()

set.

The code loops through each group and dependent boxes.

If the maximums are exceeded then a new Tab Page is created.

The code runs through with no errors, but when the form is displayed no created controls are visible, nor is the instruction to select the first TabPage obeyed.

Pretty much everything that is generated is ignored.

These are some code snippets:

VB.NET:
Public Function AddGroupBox(ByVal vTitle As String,
                                ByVal vType As GroupType,
                                Optional ByVal vInstance As Integer = 0) As System.Windows.Forms.GroupBox
        Dim iTop As Integer
        Dim iLeft As Integer
        Try

            ' Create control
            Dim oGroupBox As New System.Windows.Forms.GroupBox
            oGroupBox.Text = vTitle

            Select Case vType
                Case GroupType.gOuterBox
                    iLeft = (player.Columns * gWidth) + gOffset
                    oGroupBox.Name = Join({"GroupBox", CStr(player.Tab), CStr(player.GroupOffset)}, "_")
                    oGroupBox.Location = New System.Drawing.Point(iLeft, 9)
                    oGroupBox.Size = New System.Drawing.Size(gWidth, gMinHeight)
                    oGroupBox.Font = New Font("Arial", 11, FontStyle.Bold)
                    oGroupBox.ForeColor = oBlue
                    oGroupBox.AutoSize = True
                Case GroupType.gInnerBox
                    iTop = (vInstance * mHeight) + mOffset
                    oGroupBox.Name = Join({"MP3", CStr(player.Tab), CStr(player.GroupOffset), CStr(player.MP3Offset)}, "_")
                    oGroupBox.Location = New System.Drawing.Point(mLeft, iTop)
                    oGroupBox.Size = New System.Drawing.Size(mWidth, mHeight)
                    oGroupBox.Font = New Font("Arial", 9, FontStyle.Bold)
                    oGroupBox.ForeColor = oBlack
                    oGroupBox.AutoSize = False
                Case GroupType.gColumn
                    oGroupBox.Text = ""
                    Select Case vInstance
                        Case 1
                            iLeft = 0
                        Case 2
                            iLeft = 395
                    End Select
                    oGroupBox.Name = Join({"column", CStr(vInstance)}, "_")
                    oGroupBox.Location = New System.Drawing.Point(iLeft, 2)
                    oGroupBox.Size = New System.Drawing.Size(395, 629)
                    oGroupBox.AutoSize = False
            End Select
            oGroupBox.Visible = True
            oGroupBox.BringToFront()
            Return oGroupBox
        Catch ex As Exception

        End Try
        Return Nothing
      End Function

VB.NET:
cControl = AddGroupBox(vTitle:=sTitle,vInstance:=iOffset,vType:=GroupType.gInnerBox)
 cGroup.Controls.Add(cControl)

I've looped through all the controls in the form and ALL the created controls are there, nested inside the correct controls, but are they visible, are they heck!

I have no problem when using the Designer to add controls, but this is the first time I've been adventurous and tried to dynamically create a form...

A step too far perhaps, but it's the only way for this application...

The attached image shows the hierarchy of the nested containers.
 

Attachments

  • player.png
    player.png
    53.6 KB · Views: 32
Last edited:

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,717
Location
Sydney, Australia
Programming Experience
10+
If you can't see the controls then there are a few possible reasons, most of which can be discounted in this case:

1. They have not been added to the correct container.
2. Their Visible property, or that of a control in their hierarchy, is False.
3. Their Location is set such that they are outside the visible area of their container.
4. They are behind another control in the z-order.

Note that calling BringToFron is useless if the control has not been added to a container because there's no front to bring it to. I'd start by calling BringToFront after Add. Next I'd be looking at the Locations.
 

JA12

Member
Joined
Jul 2, 2013
Messages
17
Location
Ireland
Programming Experience
10+
Thanks for the help.

I've now got visible controls. They aren't all in the right place, but I can deal with that now I can see them!
 
Last edited:
Top Bottom