I have a windows form project ( Net 4) in C#, and I want to change all of my label.Texts in a loop. but my labels are in a Tabcontrol (in tabpage 4) and i don't know what to do. my labels name are label1 label2 and so.. until label 100.
Asked
Active
Viewed 346 times
0
-
Why so many labels? – maccettura Apr 23 '18 at 20:43
-
1Get the `TabPage` for page 4 from the `TabPages` property of the `TabControl`. Then get the Controls collection from the `TabPage`. You can loop on the `Controls` collection and look for labels. Modify each label based on its name. – Jim Rhodes Apr 23 '18 at 20:48
-
can you complete this for me? @JimRhodes tabPage4.Controls.Find("label" + i.ToString()+".Text", true) ; – Roya Nd Apr 23 '18 at 20:52
-
@maccettura do you suggest something else? i'm amatur in C#. – Roya Nd Apr 23 '18 at 20:56
-
Is this a homework assignment? – Jim Rhodes Apr 23 '18 at 20:56
-
@JimRhodes No, I'm trying to do this for myself. But I rather to do this in this way, If it's possible. – Roya Nd Apr 23 '18 at 21:01
1 Answers
0
You could use something like this to loop through the controls and set the label text based on the number in the name.
foreach ( Control ctrl in tabPage4.Controls )
{
if ( ctrl.GetType().Equals(typeof(Label)) )
{
string strName = ctrl.Name;
if ( strName.StartsWith("label", StringComparison.InvariantCultureIgnoreCase) )
{
string strNum = strName.Substring(5);
int iIndex;
if ( int.TryParse(strNum, out iIndex) )
{
ctrl.Text = "your text";
}
}
}
}

Jim Rhodes
- 5,021
- 4
- 25
- 38