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.