Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi! Last 3 hours i tried to find unfo about how to request to label not standart method.
I mean the next construction:
In loop i have variable i, that increment up to one and there is code line:

C#
label_+i.text = "Test";

How it write correctly?
So, i want change text all label that have names label_1, label_2 etc
Posted
Comments
Sergey Alexandrovich Kryukov 25-Apr-13 15:50pm    
Not clear. What is "request to label, exactly? What do you want to achieve? What is label_? i? Where you declare it?
If you are talking about UI, you should tag the UI library (WPF, Form, ASP.NET? what?), provide fully-qualified type name of "Label". A number of unrelated classes are named "Label". Which one is yours?
—SA
Pheonyx 25-Apr-13 15:50pm    
Is this a windows form application? web application? WPF? bit more info would help.
Developx 25-Apr-13 15:51pm    
I use windows forms. I want set Text for label with name label_i where i - is variable

1 solution

With a bit of casting you could do the following
C#
For(int i = 0; i < 20; i++)
{
  Control holderControl = Form.FindControl(String.Format("Label_{0}",i));
  if(holdControl != null) ((label)holderControl).Text = "My New Text";
}
 
Share this answer
 

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