Question

I've run into an interesting problem with Windows form application design that I'm having some difficulty resolving. I am currently working on a program that is supposed to be a digital replacement for a certain checklist that my company uses. This should be straightforward, but it's been mandated that the program should use the contents of an SQL database to determine what items should be on the checklist in the program (making it easy to change the items in the checklist). This means that I have to dynamically design and create much of my application using code instead of my Visual Studio 2010 designer.

I've been able to figure out the SQL stuff and even most of the dynamic design stuff, but there is one complex issue I can't seem to defeat. To create the appearance of a checklist, I dynamically create a TableLayoutPanel with two columns and one row. I then grab my data from the database and create a groupbox that is captioned with the instruction that the person using the program needs to do. I also add a textbox with a label, a checkbox, or both to the groupbox depending on the database entry. Finally, I add the textbox, label, and/or checkbox to the groupbox, then add a row to my table and add the groupbox to the table in that row. However, all of the controls in the groupbox are mashed into the top left corner of the row, so I try to adjust their organization using their .location property. Unfortunately, the moment I alter that property, the affected control disappears from the form completely. I have tried using the .bringtofront() method with no success; the control is still missing.

What I am looking for is guidance on how to organize dynamically created controls in a dynamically created groupbox (or any other grouping control) that is itself contained in another dynamically created grouping control. I would really appreciate any assistance that I can get; this problem is preventing me from getting to work on the functionality of the program. The applicable code is posted below. Thanks in advance!

Private Sub addItem(ByVal count As Integer, ByVal itemList As List(Of checklistField))
    If itemList(count).hasTextbox Or itemList(count).hasCheckbox Then
        Dim newGroupbox As New GroupBox
        With newGroupbox
            .Dock = DockStyle.Fill
            .Text = itemList(count).instruction
        End With

        If ((itemList(count).hasTextbox = True) And (itemList(count).hasCheckbox = False)) Then
            Dim newTextboxLabel As New Label
            With newTextboxLabel
                .Text = itemList(count).textboxLabel
                '.Location = New Point(20, (475))
                .AutoSize = True
            End With

            Dim newTextbox As New TextBox
            With newTextbox
                .Name = "Textbox" & count.ToString
                .Size = New Size(100, 20)
                '.Location = New Point(100, (470 + (10 * count)))
            End With

            textboxList.Add(newTextbox)
            newGroupbox.Controls.Add(newTextboxLabel)
            newGroupbox.Controls.Add(newTextbox)

            tblFields.RowCount += 1
            tblFields.RowStyles.Add(New RowStyle(SizeType.AutoSize))
            tblFields.Controls.Add(newGroupbox, 0, tblFields.RowCount - 1)
Was it helpful?

Solution

Your commented out Location properties have the Y value set too high, so the controls are not visible on the form:

'.Location = New Point(20, (475))
'.Location = New Point(100, (470 + (10 * count)))

The Location of the control is in relation to the parent's client space, so use smaller numbers:

With newTextboxLabel
  .Text = itemList(count).textboxLabel
  .Location = New Point(20, 20)
  .AutoSize = True
End With

Dim newTextbox As New TextBox
With newTextbox
  .Name = "Textbox" & count.ToString
  .Size = New Size(100, 20)
  .Location = New Point(20, 40)
End With
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top