Click here to Skip to main content
11,496,146 members (683 online)
The site is currently in read-only mode for maintenance. Posting of new items will be available again shortly.
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
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?
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

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";

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

  Print Answers RSS
0 RyanDev 230
1 Sergey Alexandrovich Kryukov 114
2 Richard Deeming 100
3 OriginalGriff 89
4 Mostafa Asaduzzaman 59
0 Sergey Alexandrovich Kryukov 10,401
1 OriginalGriff 8,910
2 Sascha Lefèvre 3,899
3 Maciej Los 3,422
4 Richard Deeming 2,600

Advertise | Privacy | Mobile
Web01 | 2.8.150520.1 | Last Updated 25 Apr 2013
Copyright © CodeProject, 1999-2015
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