Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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:
 
label_+i.text = "Test";
How it write correctly?
So, i want change text all label that have names label_1, label_2 etc
Posted 25-Apr-13 10:36am
Comments
Sergey Alexandrovich Kryukov at 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 at 25-Apr-13 15:50pm
   
Is this a windows form application? web application? WPF? bit more info would help.
Developx at 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

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

With a bit of casting you could do the following
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";
}
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 400
1 Sergey Alexandrovich Kryukov 329
2 Afzaal Ahmad Zeeshan 264
3 BillWoodruff 245
4 CPallini 195
0 OriginalGriff 5,560
1 DamithSL 4,476
2 Maciej Los 3,942
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,175


Advertise | Privacy | Mobile
Web03 | 2.8.141216.1 | Last Updated 25 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100