Tablelayoutpanel not displaying children controls when looking at it or at the start of the program

I made a program that reads a .json object and creates labels and some controls in three TableLayoutPanels. The first two tables are in a TabPanel’s page 1, and the last is in page 2.

The problem is: IF at the start of the program I automatically add controls to the tables OR if I am looking at page 2, and add the controls manually, the last table doesnt display the added controls.
I can Console Log and it does have all the controls, it just doesnt display them.

The table only works IF I’m on page 1 and add the controls manually. In all three cases, the code is the same for adding the controls. It just behaves differently if I am looking at page 2 or not, or if it runs at start up. Doesn’t make sense to me.

Gif 1 how it should work: in this case, I am on page 1 and I manually load the json file to add the controls

Gif 2 I open the program and it automatically loads the json and add the controls. The controls on the page 2’s LayoutTable aren’t showing.

Gif 3 I am ‘looking’ at page 2’s table and I load the json file. The controls also aren’t shown.

I’ve already tried calling tablelayoutpanel.Refresh() or .Invalidade(), doesnt work. Tried setting doublebuffered true and false, doesnt work.

ps: “manually adding the controls” means I click on a button to load the json and the controls are added.

Code that adds the controls to the panels:

LabelData labelData;

List<LabelFieldData> publicZPLFields = new List<LabelFieldData>();
List<Control> publicControls = new List<Control>();

List<LabelFieldData> privateZPLFields = new List<LabelFieldData>();
List<Control> privateControls = new List<Control>();
List<Control> privateDataControls = new List<Control>();

List<LabelFieldData> dataMatrixFields = new List<LabelFieldData>();
List<Control> dataMatrixControls = new List<Control>();

public void LoadLabelData(LabelData data)
{
    #region clear lists and dispose controls
    for (int i = 0; i < publicControls.Count; i++)
    {
        publicTablePanel.Controls.Remove(publicControls[i]);
        publicControls[i].Dispose();
    }
    publicControls.Clear();

    for (int i = 0; i < privateControls.Count; i++)
    {
        privateTablePanel.Controls.Remove(privateControls[i]);
        privateControls[i].Dispose();
    }
    privateControls.Clear();

    for (int i = 0; i < privateDataControls.Count; i++)
    {
        privateDataTablePanel.Controls.Remove(privateDataControls[i]);
        privateDataControls[i].Dispose();
    }
    privateDataControls.Clear();

    dataMatrixControls.Clear();
    #endregion

    labelData = data;

    publicZPLFields = data.LabelDataList.Where(d => !d.IsPrivate &&  d.ListMe).ToList();
    privateZPLFields = data.LabelDataList.Where(d => d.IsPrivate && d.ListMe).ToList();
    dataMatrixFields = data.LabelDataList.Where(d => d.Visibility == FieldVisibility.DM || d.Visibility == FieldVisibility.BOTH).ToList();

    AnchorStyles a = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top | AnchorStyles.Bottom;
    Padding m = new Padding(1);
    
    //adding the controls to the first table layout panel in the first page
    int row = 1;
    foreach (var d in publicZPLFields)
    {
        Label label = new Label();
        label.Text = d.Name;
        label.TextAlign = ContentAlignment.MiddleLeft;
        publicTablePanel.Controls.Add(label, 0, row);
        
        publicControls.Add(label);

        switch (d.FieldType)
        {
            case FieldType.Text:
                TextBox tb = new TextBox();
                tb.Name = d.Name;
                tb.Margin = m;
                tb.Anchor = a;
                tb.Text = d.defaultText;
                tb.KeyPress += textBox_KeyPressed;
                tb.TextChanged += updateZpl_TextChanged;
                publicTablePanel.Controls.Add(tb, 1, row);
                publicControls.Add(tb);

                if(d.Visibility == FieldVisibility.DM || d.Visibility == FieldVisibility.BOTH)
                    dataMatrixControls.Add(tb);
                break;
            case FieldType.Date:
                DateTimePicker dtp = new DateTimePicker();
                dtp.Name = d.Name;
                dtp.Margin = m;
                dtp.Anchor = a;
                dtp.Value = d.DateType == FieldDateType.TODAY ? DateTime.Today : d.defaultTime;
                if(d.DateType == FieldDateType.TODAY)
                    dtp.Enabled = false;
                publicTablePanel.Controls.Add(dtp, 1, row);
                publicControls.Add(dtp);

                if (d.Visibility == FieldVisibility.DM || d.Visibility == FieldVisibility.BOTH)
                    dataMatrixControls.Add(dtp);
                break;
            case FieldType.Serial:
                NumericUpDown nud = new NumericUpDown();
                nud.Name = d.Name;
                nud.Margin = m;
                nud.Anchor = a;
                nud.Maximum = (int)Math.Pow(10, d.serialMax) - 1;
                nud.Value = d.serialStart;
                publicTablePanel.Controls.Add(nud, 1, row);
                publicControls.Add(nud);

                if (d.Visibility == FieldVisibility.DM || d.Visibility == FieldVisibility.BOTH)
                    dataMatrixControls.Add(nud);
                break;
        }

        row++;
    }
    //adding controls to both "private" table layout panel, the one in the first page and the the one in the second page
    row = 1;
    foreach(var d in privateZPLFields)
    {
        Label label = new Label();
        label.Text = d.Name;
        label.TextAlign = ContentAlignment.MiddleLeft;
        privateTablePanel.Controls.Add(label, 0, row);
        privateControls.Add(label);

        label = label.Clone();
        privateDataTablePanel.Controls.Add(label, 0, row);
        privateDataControls.Add(label);

        switch (d.FieldType)
        {
            case FieldType.Text:
                TextBox tb = new TextBox();
                tb.Name = d.Name;
                tb.Margin = m;
                tb.Anchor = a;
                tb.Text = d.defaultText;
                tb.Enabled = false;
                privateTablePanel.Controls.Add(tb, 1, row);
                privateControls.Add(tb);

                tb = tb.Clone();
                tb.Enabled = true;
                tb.KeyPress += textBox_KeyPressed;
                tb.TextChanged += updateZpl_TextChanged;
                privateDataTablePanel.Controls.Add(tb, 1, row);
                privateDataControls.Add(tb);

                if (d.Visibility == FieldVisibility.DM || d.Visibility == FieldVisibility.BOTH)
                    dataMatrixControls.Add(tb);
                break;
            case FieldType.Date:
                DateTimePicker dtp = new DateTimePicker();
                dtp.Name = d.Name;
                dtp.Margin = m;
                dtp.Anchor = a;
                dtp.Value = d.DateType == FieldDateType.TODAY ? DateTime.Today : d.defaultTime;
                dtp.Enabled = false;
                privateTablePanel.Controls.Add(dtp, 1, row);
                privateControls.Add(dtp);

                dtp = dtp.Clone();
                dtp.Enabled = d.DateType == FieldDateType.CUSTOM;
                privateDataTablePanel.Controls.Add(dtp, 1, row);
                privateDataControls.Add(dtp);

                if (d.Visibility == FieldVisibility.DM || d.Visibility == FieldVisibility.BOTH)
                    dataMatrixControls.Add(dtp);
                break;
            case FieldType.Serial:
                NumericUpDown nud = new NumericUpDown();
                nud.Name = d.Name;
                nud.Margin = m;
                nud.Anchor = a;
                nud.Maximum = (int)Math.Pow(10, d.serialMax) - 1;
                nud.Value = d.serialStart;
                nud.Enabled = false;
                privateTablePanel.Controls.Add(nud, 1, row);
                privateControls.Add(nud);

                nud = nud.Clone();
                nud.Enabled = true;
                privateDataTablePanel.Controls.Add(nud, 1, row);
                privateDataControls.Add(nud);

                if (d.Visibility == FieldVisibility.DM || d.Visibility == FieldVisibility.BOTH)
                    dataMatrixControls.Add(nud);
                break;
        }
        row++;
    }

    UpdateFinalZPL(labelData.sampleCode);

    #region setup table layout panels
    publicTablePanel.AutoScroll = true;
    privateTablePanel.AutoScroll = true;
    privateDataTablePanel.AutoScroll = true;
    #endregion
}

3

I managed to fix it. Basically, I only have two forloops, one to add controls to one table, and other to add controls to the remaining two tables.
I separated this second loop in two, for each table, and now it works.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật