Create labels at runtime
You need to add the labels to the Controls
collection of the tab page:
tabPage2.Controls.Add(CustomLabel[CustomLabel.Count - 1] as Control);
BTW: You shouldn't be using ArrayList
. Instead use a List<Label>
. Furthermore, first initialize the label, than add it to the list. This makes your code a lot more readable:
List<Label> customLabels = new List<Label>();
foreach (string ValutaCustomScelta in Properties.Settings.Default.ValuteCustom)
{
Label label = new Label();
label.Location = new System.Drawing.Point(317, 119 + customLabels.Count*26);
label.Parent = tabPage2;
label.Name = "label" + ValutaCustomScelta;
label.Text = ValutaCustomScelta;
label.Size = new System.Drawing.Size(77, 21);
customLabels.Add(label);
tabPage2.Controls.Add(label);
}
Vincenzo Lo Palo
Catholic, Apostolic, Roman. Enthusiastic Coder and 3D Artist with the aim of spreading the Gospel. I I love Jesus Christ and Mary! Thanks my Lord because you are my Creator and I'm your creature! JHS Anima Christi, sanctifica me. Corpus Christi, salva me. Sanguis Christi, inebria me. Aqua lateris Christi, lava me. Passio Christi, conforta me. O bone Jesu, exaudi me. Intra vulnera tua absconde me. Ne permittas me separari a Te. Ab hoste maligno defende me. In hora mortis meae voca me, Et jube me venire ad Te, Ut cum Sanctis tuis laudem Te In saecula saeculorum.
Updated on January 22, 2020Comments
-
Vincenzo Lo Palo over 4 years
With this code I can create labels at runtime:
ArrayList CustomLabel = new ArrayList(); foreach (string ValutaCustomScelta in Properties.Settings.Default.ValuteCustom) { CustomLabel.Add(new Label()); (CustomLabel[CustomLabel.Count - 1] as Label).Location = new System.Drawing.Point(317, 119 + CustomLabel.Count*26); (CustomLabel[CustomLabel.Count - 1] as Label).Parent = tabPage2; (CustomLabel[CustomLabel.Count - 1] as Label).Name = "label" + ValutaCustomScelta; (CustomLabel[CustomLabel.Count - 1] as Label).Text = ValutaCustomScelta; (CustomLabel[CustomLabel.Count - 1] as Label).Size = new System.Drawing.Size(77, 21); Controls.Add(CustomLabel[CustomLabel.Count - 1] as Control); }
I need create labels on tabPage2, but this row not work:
(CustomLabel[CustomLabel.Count - 1] as Label).Parent = tabPage2;
Which is the correct instruction to create label on tabPage2 at runtime? (Im using visual studio 2010, windows form)
-
Vincenzo Lo Palo over 11 yearsDaniel, only a little problem: in tabPage 2 I have an groupbox that hide labels created. How Can I place labels in "front of groupbox"?
-
Daniel Hilgarth over 11 years@vincenzolopalo: You can use
tabPage2.SetChildIndex(label, 0)
or actually add the labels to the group box:tabPage2.YourGroupBox.Controls.Add(label)
. Make sure that you specify the group box to be public in its properties. -
Vincenzo Lo Palo over 11 yearsI tryed but not work: tabPage2.groupBox3.Controls.Add(label); in this way work: tabPage2.Controls.SetChildIndex(label, 0); thanks for your important support Daniel!
-
Daniel Hilgarth over 11 years@vincenzolopalo:
tabPage2.groupBox3.Controls.Add(label);
should work if you make sure theModifier
property of the group box is set to Public in the Properties window inside the designer. -
Vincenzo Lo Palo over 11 yearsI did so (public modifiers of groupBox3), but tabPage2.groupBox3... get me error in visual Studio 2010.
-
Daniel Hilgarth over 11 years@vincenzolopalo: Yes, you are correct.
groupBox3
won't be created as a variable oftabPage2
. Try this:groupBox3.Controls.Add(label);