Click here to Skip to main content
15,920,111 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi all
I was wondering how you would go about using an object name in the code.
Example i have a number of labels naming lbl001, lbl002 etc.
In my code, say if i want to change all the label text, how would i change them easily?
ie
C#
for (int 1=0;i<20;i++)
{
lbl + i.tostring().padleft(0,3) .text - "Hello world";
} 


TIA
Posted
Updated 8-Mar-15 19:23pm
v2
Comments
Sergey Alexandrovich Kryukov 9-Mar-15 1:25am    
First of all, don't write code samples which cannot compile. Write real code, copy what you really write in Visual Studio. Please fix it using "Improve question".
Show two examples: before change and after change, to show what you want to achieve.
So far, the question is unclear, it also pretty likely that it makes no sense. You subtracting a string from as string, it can't make any sense.
—SA

Hi,

You may use like following ..

C#
foreach(Control ctl in this.Controls)
{
if(ctl.GetType()==typeof(Label))
{
ctl.Text="Hello World";
}
}


Thanks
Magesh.M
 
Share this answer
 
Try
C#
foreach (Control c in parent.Controls)
{
    int cnt = 1;
    if (c.GetType() == typeof(Label)) {
        if (c.Name == "lbl" + cnt)
        {
            //Do Stuff with the control
            cnt++;
        }
    }
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900